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.

4
  • 4
    On which line exactly? Read: What is a NullReferenceException and how do I fix it? Commented Oct 21, 2015 at 6: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. Commented Oct 21, 2015 at 6:49
  • in populatesubject method. where written "con.connectionstring=configurationmanager..." Commented Oct 21, 2015 at 6: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 Commented Oct 21, 2015 at 6:53

2 Answers 2

6

Remove the white space in Web.config:

<add name=" constr" ...

To

<add name="constr" ...
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.