aspx update button not updating all fields

Walter Graulich 0 Reputation points
2024-06-20T14:40:27.4766667+00:00

I have an update button and when clicked, it updates all the fields in my sql table except for the "Channel" field.

Capture

protected void btnupdate_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && DropDownList1.SelectedValue != "")
            {
                using (con = new SqlConnection(cs))
                {
                    con.Open();
                    cmd = new SqlCommand("Update Assigned_Radio_Channels Set Channel=@Channel, Agency_Assigned_To_Channel=@Agency_Assigned_To_Channel, Dates=@Dates, Assigned_By=@Assigned_By, Notes=@Notes where Channel=@Channel", con);
                    cmd.Parameters.AddWithValue("@Channel", DropDownList1.SelectedValue);
                    cmd.Parameters.AddWithValue("@Agency_Assigned_To_Channel", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@Dates", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@Assigned_By", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@Notes", TextBox4.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    DataLoad();
                    ClearAllData();
                }
            }
            else
            {
                lblMessage.Text = "Fill In All Information";
            }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,386 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,936 Reputation points
    2024-06-20T15:07:19.1666667+00:00

    Try getting the value of the selected item.

    DropDownList1.SelectedItem.Value
    

    Reference Docs

    https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.dropdownlist?view=netframework-4.8.1


  2. Walter Graulich 0 Reputation points
    2024-06-20T15:56:20.8566667+00:00

    I needed to add the ID field to make it work

    protected void btnupdate_Click(object sender, EventArgs e)
            {
                if (TextBox1.Text != "" && TextBox2.Text != "" && TextBox3.Text != "" && DropDownList1.SelectedValue != "")
                {
                    using (con = new SqlConnection(cs))
                    {
                        con.Open();
                        cmd = new SqlCommand("Update Assigned_Radio_Channels Set Channel=@Channel, Agency_Assigned_To_Channel=@Agency_Assigned_To_Channel, Dates=@Dates, Assigned_By=@Assigned_By, Notes=@Notes where ID=@ID", con);
                        cmd.Parameters.AddWithValue("@Channel", DropDownList1.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@Agency_Assigned_To_Channel", TextBox1.Text);
                        cmd.Parameters.AddWithValue("@Dates", TextBox2.Text);
                        cmd.Parameters.AddWithValue("@Assigned_By", TextBox3.Text);
                        cmd.Parameters.AddWithValue("@Notes", TextBox4.Text);
                        cmd.Parameters.AddWithValue("@ID", TextBox5.Text);
                        cmd.ExecuteNonQuery();
                        con.Close();
                        DataLoad();
                        ClearAllData();
                    }
                }
                else
                {
                    lblMessage.Text = "Fill In All Information";
                }
            }