Try getting the value of the selected item.
DropDownList1.SelectedItem.Value
Reference Docs
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have an update button and when clicked, it updates all the fields in my sql table except for the "Channel" field.
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";
}
Try getting the value of the selected item.
DropDownList1.SelectedItem.Value
Reference Docs
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";
}
}