1

I had this aspx source code

<tr>
            <td>MatchName</td>
            <td>
                <asp:DropDownList ID="ddlMatchName" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>Winning Team</td>
            <td>
                <asp:DropDownList ID="ddlWinningTeam" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>Score</td>
            <td>
                <asp:TextBox ID="txtScore" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnSave" runat="server" Text="Save"  OnClick="btnSave_Click" />
            </td>
            <td>
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
            </td>
        </tr>

C# Code

 protected void Page_Load(object sender, EventArgs e)
    {
        string queryMatchDetail = "select MatchId,MatchName from ADMMasterMatch order by MatchDate";
        ddlMatchName.DataSource = clsNewSqlFunctions.GetSelectedData(queryMatchDetail);
        ddlMatchName.DataTextField = "MatchName";
        ddlMatchName.DataValueField = "MatchId";
        ddlMatchName.DataBind();

        string queryTeamDetail = "SELECT [TeamId],[TeamName] FROM [AdmCurrentEventMasterTeam] order by TeamName";
        ddlWinningTeam.DataSource = clsNewSqlFunctions.GetSelectedData(queryTeamDetail);
        ddlWinningTeam.DataTextField = "TeamName";
        //ddlWinningTeam.DataValueField = "TeamId";
        ddlWinningTeam.DataBind();

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        var s = ddlMatchName.SelectedIndex;
        string query = "UPDATE [ADMMasterMatch] SET Winner = '" + ddlWinningTeam.SelectedItem.Text + "' , Score = '" + txtScore.Text + "' Where MatchId ='" + ddlMatchName.SelectedItem.Text +"'";
        clsNewSqlFunctions.ExecuteQuery(query);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ADMCurrentEventMatchResult.aspx");
    }

Here clsNewSqlFunctions.ExecuteQuery() and clsNewSqlFunctions.GetSelectedData() are method to execute query and select data which return data in form of dataset

on Save_Click event i am updating data on database from selected value of dropdown list but it is not showing me selected value
It always show value at index 0. on SelectedText,SelectedItem
please help me what is wrong with my code

Valamas
  • 24,169
  • 25
  • 107
  • 177
Amit Bisht
  • 4,870
  • 14
  • 54
  • 83
  • 1
    It's because you re-bind your list on the postback - look up the page lifecycle and the `IsPostBack` property. –  May 14 '14 at 07:52
  • Your `btnSave_Click` query is ripe for a sql injection attack. –  May 14 '14 at 07:55
  • possible duplicate of [DropDownList lose index after PostBack](http://stackoverflow.com/questions/17742605/dropdownlist-lose-index-after-postback) – Elad Lachmi May 14 '14 at 08:29
  • @DaveParsons what does it mean and how could i correct it – Amit Bisht May 15 '14 at 05:23
  • @Co.Aden: It means a malicious user can "inject" a sql statement which will be run on your DB. `Parameterised queries` or `stored procedures` are the ways to mitigate this risk. –  May 15 '14 at 07:45

1 Answers1

2

Please bind your dropdown in !ispostback

 protected void Page_Load(object sender, EventArgs e)
    {
      if(!ispostback)
      {
        string queryMatchDetail = "select MatchId,MatchName from ADMMasterMatch order by MatchDate";
        ddlMatchName.DataSource = clsNewSqlFunctions.GetSelectedData(queryMatchDetail);
        ddlMatchName.DataTextField = "MatchName";
        ddlMatchName.DataValueField = "MatchId";
        ddlMatchName.DataBind();

        string queryTeamDetail = "SELECT [TeamId],[TeamName] FROM [AdmCurrentEventMasterTeam] order by TeamName";
        ddlWinningTeam.DataSource = clsNewSqlFunctions.GetSelectedData(queryTeamDetail);
        ddlWinningTeam.DataTextField = "TeamName";
        //ddlWinningTeam.DataValueField = "TeamId";
        ddlWinningTeam.DataBind();
      }

    }

When you click a button page again postbacks and which will result in binding the dropdown again..So you will get the selected value as the first index of the dropdown..

Jameem
  • 1,840
  • 3
  • 15
  • 26