简体   繁体   English

SQL Server 2014中的NullReferenceException

[英]NullReferenceException in SQL Server 2014

I want to save checkboxlist data in a database. 我想将复选框列表数据保存在数据库中。 I created a table in SQL named 'tblSubject' and made connectionstring in web.config. 我在SQL中创建了一个名为“ tblSubject”的表,并在web.config中创建了连接字符串。 However I still get the erorr : 但是我仍然得到erorr:

NullReferenceException was unhandled by user code 用户代码未处理NullReferenceException
object reference not set to an instance of an object. 你调用的对象是空的。

This is the code in c#: 这是c#中的代码:

 private void PopulateSubjects()
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select * from subjects";
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = sdr["Subject"].ToString();
                    item.Value = sdr["Id"].ToString();
                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                    chbox.Items.Add(item);
                }
            }
            conn.Close();
        }
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "update subjects set IsSelected = @IsSelected" +
                              " where Id=@Id";
            cmd.Connection = conn;
            conn.Open();
            foreach (ListItem item in chbox.Items)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@IsSelected", item.Selected);
                cmd.Parameters.AddWithValue("@Id", item.Value);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

}

And in Web.config: 并在Web.config中:

 <connectionStrings>
  <add name=" constr" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;
  Initial Catalog = dbtblSubject; Integrated Security = true" providerName="System.Data.SqlClient" />
 </connectionStrings>

Any help would be much appreciated. 任何帮助将非常感激。

Remove the white space in Web.config : 删除Web.configwhite space

<add name=" constr" ...

To

<add name="constr" ...

First of all, you should look at your stack trace for where the nullreference occurs. 首先,您应该查看堆栈跟踪中是否存在nullreference。

It looks like you're on the right track, thinking that the connection string is the cause of this exception. 您似乎在正确的轨道上,认为连接字符串是导致此异常的原因。 If you look at your Web.config, the name of the connection string is " constr", with an extra space in the start. 如果您查看Web.config,则连接字符串的名称为“ constr”,开始处有多余的空格。 This does not match your code: 这与您的代码不匹配:

conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

Remove the first space in the connection string name in Web.Config, and your code will probably work. 删除Web.Config中的连接字符串名称中的第一个空格,您的代码可能会起作用。

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

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