gpt4 book ai didi

c# - 对象引用未设置为对象的实例(不知道我应该做什么)

转载 作者:行者123 更新时间:2023-12-02 14:00:06 24 4
gpt4 key购买 nike

这是错误:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

它停在这里:con.Open();

这是代码:

SqlConnection con = new SqlConnection(DBHelper.connection);
SqlCommand com = new SqlCommand();
con = com.Connection;
con.Open();
com.CommandType = CommandType.Text;
com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue ;
SqlDataReader dr= com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
DataRow drr;
drr=dt.Rows[0];
con.Close();

错误:

Line 19:         SqlCommand com = new SqlCommand();
Line 20: con = com.Connection;
Line 21: con.Open(); // here the error
Line 22: com.CommandType = CommandType.Text;
Line 23: com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue

最佳答案

第三行是错误的。应该是

  com.Connection = con;

关于c# - 对象引用未设置为对象的实例(不知道我应该做什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com