简体   繁体   中英

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. However I still get the erorr :

NullReferenceException was unhandled by user code
object reference not set to an instance of an object.

This is the code in 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:

 <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 :

<add name=" constr" ...

To

<add name="constr" ...

First of all, you should look at your stack trace for where the nullreference occurs.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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