简体   繁体   English

asp.net dropDownList选定的值null

[英]asp.net dropDownList selected value null

I am making a school application about articles publication. 我正在申请有关文章发表的学校申请。 In a dropdownList I want to display the value that exists in database as default pre-selected value. 在dropdownList中,我想显示数据库中存在的值作为默认的预选值。 DropdownList conatins "emertimi" from table "kategorite". DropdownList从表“ kategorite”隐含“ emertimi”。 When user selects a value it saves id of "kategoria_id" in table "artikulli". 当用户选择一个值时,它将在表“ artikulli”中保存“ kategoria_id”的id。 Here is my code behind 这是我的代码背后

if (e.Item.ItemType == ListItemType.EditItem)
            {
                DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
                SqlConnection con = new SqlConnection(connection);
                string Qry = "select * from kategoria";
                string id = Request.QueryString["id"];
                SqlDataAdapter da = new SqlDataAdapter(Qry, con);
                DataSet ds = new DataSet();
                DataSet ds2 = new DataSet();
                DataSet ds3 = new DataSet();
                con.Open();
                da.Fill(ds);
                string kategoria_id = "select kategoria_id from artikulli where id='" + id + "'";
                SqlDataAdapter dk = new SqlDataAdapter(kategoria_id, con);
                dk.Fill(ds2);
                var kategoria_id_result = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                string emertimi = "select emertimi from kategoria where id='" + kategoria_id_result + "'";
                SqlDataAdapter de = new SqlDataAdapter(emertimi, con);
                de.Fill(ds3);

                drpdKategoria.DataSource = ds;
                drpdKategoria.DataValueField = "id";
                drpdKategoria.DataTextField = "emertimi";              
                drpdKategoria.DataBind();
                drpdKategoria.SelectedValue = drpdKategoria.Items.FindByText(emertimi).Value;
                con.Close();
                con.Dispose();
                ds.Dispose();
                ds2.Dispose();
                ds3.Dispose();
                da.Dispose();
                dk.Dispose();
                de.Dispose();
            }
        }

But it's showing this error: Object reference not set to an instance of an object . 但它显示了此错误: 对象引用未设置为object的实例 at this line: drpdKategoria.SelectedValue = drpdKategoria.Items.FindByText(emertimi).Value; 在这一行: drpdKategoria.SelectedValue = drpdKategoria.Items.FindByText(emertimi).Value;

I guess emertimi contains a value that cannot be found in the dropdown list. 我猜emertimi包含一个在下拉列表中找不到的值。 Therefore, the FindByText will return null and getting the Value will result in this exception. 因此, FindByText将返回null并且获取Value将导致此异常。

Test the result before you try to get the Value . 在尝试获取Value之前,请测试结果。

Also, instead of the separate Dispose calls, use using statements. 另外,使用using语句代替单独的Dispose调用。 And be aware of the SQL injection risk: use parameters instead. 并且要注意SQL注入的风险:请改用参数。

That's because the item you're looking for has not been found. 那是因为找不到您要寻找的商品。 You should troubleshoot your code and figure out what emertimi is set to at run time. 您应该对代码进行故障排除,并弄清楚在运行时设置了什么emertimi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM