0

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.

programmer21
  • 163
  • 9
  • 4
    On which line exactly? Read: [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Oct 21 '15 at 06:48
  • Please look at your Exception StackTrace, and see where the NullReference is. This should give you some pointers on where to start looking for an error in the code. – Kjetil Oct 21 '15 at 06:49
  • in populatesubject method. where written "con.connectionstring=configurationmanager..." – programmer21 Oct 21 '15 at 06:51
  • 1
    the name in web.config contains a leading space " constr"... maybe that's the problem (or is it a typo in your question?). Because you want to get it by name without space : `ConfigurationManager.ConnectionStrings["constr"].ConnectionString` – CeOnSql Oct 21 '15 at 06:53

2 Answers2

6

Remove the white space in Web.config:

<add name=" constr" ...

To

<add name="constr" ...
Waqar Ahmed
  • 1,327
  • 1
  • 10
  • 30
3

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.

Kjetil
  • 474
  • 4
  • 15