I solve a simple task on asp net (web app !!!). Faced a very incomprehensible problem for me when editing an item in the grid view. When I switch to edit mode, I have a field - drop-down list Example 
Change text fields and department drop-down list. Next in the GridView1_RowUpdating method,
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; TextBox Last_Name = (TextBox)row.FindControl("tb_Last_Name"); TextBox First_Name = (TextBox)row.FindControl("tb_First_Name"); TextBox Second_Name = (TextBox)row.FindControl("tb_Second_Name"); Label lbl_ID_Empl = (Label)row.FindControl("lbl_ID_Employee"); Label lbl_ID_Dep = (Label)row.FindControl("lbl_ID_Department"); DropDownList ddl_Department = (DropDownList)row.FindControl("DropDownList1"); string commandTextUpdEmpl = String.Format(@"UPDATE [Employee] SET ID_Department = {0} , First_Name = '{1}', Second_Name = '{2}', Last_Name = '{3}' WHERE[ID_Employee] = {4}", ddl_Department.SelectedValue, First_Name.Text, Second_Name.Text, Last_Name.Text, lbl_ID_Empl.Text); SqlCommand cmd = new SqlCommand(commandTextUpdEmpl, conn); conn.Open(); //SqlCommand cmd = new SqlCommand(@"UPDATE [Employee] SET [Last_Name] = @Last_Name, // First_Name = @First_Name, /*comment*/ Second_Name = @Second_Name, // ID_Department = @ID_Department // WHERE[ID_Employee] = @ID_Employee", conn); //cmd.Parameters.Add("@Last_Name", SqlDbType.NChar).Value = Last_Name; //cmd.Parameters.Add("@First_Name", SqlDbType.NChar).Value = First_Name; //cmd.Parameters.Add("@Second_Name", SqlDbType.NChar).Value = Second_Name; //cmd.Parameters.Add("@ID_Department", SqlDbType.Int).Value = Convert.ToInt32(lbl_ID_Dep.Text); //cmd.Parameters.Add("@ID_Employee", SqlDbType.Int).Value = Convert.ToInt32(lbl_ID_Empl.Text); Label5.Text = Last_Name.Text + First_Name.Text + Second_Name.Text + ddl_Department.SelectedValue + " " + lbl_ID_Dep.Text + " " + lbl_ID_Empl.Text; Label5.Text = cmd.CommandText; cmd.ExecuteNonQuery(); GridView1.EditIndex = -1; GridView1.DataBind(); conn.Close(); } especially the text SqlCommand.CommandText I bring to the label on the form (for verification). I receive:
UPDATE [Employee] SET ID_Department = 4, First_Name = 'Stanislav mod', Second_Name = 'Antonovich mod', Last_Name = 'Bulls mod' WHERE [ID_Employee] = 25
That is, it is formed correctly inside the method, BUT this Update does not get into the processing of SQL server (I checked it through Profiler). This is the main problem why it is not transmitted to the server from the method. !!! When update to sql server, a request is generated that is formed on the Page1_GridView.aspx page in DataSource1, in which the value from the ID_Department field is taken as a parameter, but not from the drop-down list.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeesConnectionString %>" DeleteCommand=" delete from Employee where ID_Employee = @ID_Employee " InsertCommand=" insert into Employee values (@Last_Name, @First_Name, @Second_Name, @ID_Department) " SelectCommand="select d.ID_Department, d.Name, e.Last_Name, e.First_Name,e.Second_Name, e.ID_Employee from Department d left join Employee e on d.ID_Department = e.ID_Department" UpdateCommand=" UPDATE [Employee] SET [Last_Name] = @Last_Name, First_Name=@First_Name, Second_Name=@Second_Name, ID_Department = @ID_Department WHERE [ID_Employee] = @ID_Employee "> <DeleteParameters> <asp:Parameter Name="ID_Employee" Type="Int16" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="Last_Name" Type="String" /> <asp:Parameter Name="First_Name" Type="String" /> <asp:Parameter Name="Second_Name" Type="String" /> <asp:Parameter Name="ID_Department" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="Last_Name" Type="String" /> <asp:Parameter Name="First_Name" Type="String" /> <asp:Parameter Name="Second_Name" Type="String" /> <asp:Parameter Name="ID_Employee" Type="Int16" /> <asp:Parameter Name="ID_Department" Type="Int16" /> </UpdateParameters> </asp:SqlDataSource> That is, the problem is clear, my main question is, why does UPDATE from SqlDataSource1 get into processing, and not from a method in code? How to act in this case, it is necessary either to do something so that the parameter from the drop-down list is passed to the SqlDataSource1 or to make the UPDATE not work in the SqlDataSource1, but work out the update from the GridView1_RowUpdating method. That is, as a beginner, I don’t understand this logic of the work, or I’m probably missing some nuances, I’ll be very grateful if someone tells me how to solve this problem correctly.
