I have a DropdownList
Code
Based on the selected text I populate another ddl with
C#
protected void ddlComponentaActivitate_SelectedIndexChanged(Object sender, EventArgs e)
{
ddlFactorRisc.Items.Clear();
ddlFactorRisc.Items.Add("Factor de risc");
using (SqlConnection conn = new SqlConnection(connString))
{
string sqlQuery = "SELECT FactorRisc, FRFID FROM tblFactoriRiscFactori WHERE FRCID = @FRCID ORDER BY FactorRisc";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FRCID", ddlComponentaActivitate.SelectedItem.Value);
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
ddlFactorRisc.DataSource = ds.Tables[0];
ddlFactorRisc.DataBind();
conn.Close();
}
}
ActiveTAB.Value = "#TabEvaluare";
}The second ddl on View with
Code
But when I try to add data to table using FactorRisc as parameter
C#
cmd.Parameters.AddWithValue("@FactorRisc", ddlFactorRisc.SelectedItem.Text);I have error that say cannot add null. So it seems it will not get the value. Worth to mention I have 2 more ddl on page, one listed below
Code
On screen data is visible but error on postback. Why?


Marius VasileRank #785 · 2.9K reputationPosted Jun 16, 2024, 2:22 PM EDT
I tried to store the values with jquery and it works
$("#ddlFactorRisc").on("change", function () { var fr = $("#ddlFactorRisc option:selected").text(); var frID = $("#ddlFactorRisc option:selected").val(); $("#txtFR").val(fr); $("#txtFRID").val(frID); });but why not directly from ddl with
cmd.Parameters.AddWithValue("@FactorRisc", ddlFactorRisc.SelectedItem.Text); cmd.Parameters.AddWithValue("@FRFID", ddlFactorRisc.SelectedItem.Value);