Connection String Error: Object reference not set to an instance of an object

indradeo
 
on Jul 02, 2020 12:36 AM
1545 Views

while usere click on check box and click on submit button then Error occured: Object reference not set to an instance of an object.

        public void btnSendMail_Click(object sender, System.EventArgs e)
        {           
            string Id = string.Empty;
            DataTable dt = new DataTable();
            try
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                   
                    Id = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);

                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                    string flag = cb.Checked ? "Y" : "N";
                    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        SqlCommand cmd = new SqlCommand("UPDATE employee SET flag = @flag where Id=@Id");
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@flag", flag);
                        cmd.Parameters.AddWithValue("@Id", Id);
                        con.Open();
                        string isUpdated = Convert.ToString(cmd.ExecuteNonQuery());
                        con.Close();
                    }
                    if (cb.Checked)
                    {
                        int count = 5000;
                        Label[] labels1 = new Label[count];
                        Label[] labels2 = new Label[count];
                        Label[] labels3 = new Label[count];
                        string[] temp1 = new string[count];
                        for (int i = 0; i < count; i++)
                        {
                            labels1[i] = new Label();
                            labels2[i] = new Label();
                            labels3[i] = new Label();
                           
                        }

                      
                        for (int i = 0; i < count; i++)
                        {
                            this.Controls.Add(labels1[i]);
                            this.Controls.Add(labels2[i]);
                            this.Controls.Add(labels3[i]);
                            
                        }

                        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                        SqlConnection con = new SqlConnection(connStr);
                        con.Open();
                        SqlCommand cmd = new SqlCommand("select Workman_ID,name,Contractor_Name from employee where flag='N' ");
                        cmd.CommandType = System.Data.CommandType.Text;
                        cmd.Connection = con;
                        string temp = " ";
                       
                        int j = 0;
                        SqlDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            temp = reader["Workman_ID"].ToString() + "<br/>";
                            temp += reader["Name"].ToString() + "<br/>";
                            temp += reader["Contractor_Name"].ToString() + "<br/>";


                
                labels1[j].Text = temp;
                
                labels2[j].Controls.Add(printbarcode(reader["Workman_ID"].ToString()));

                labels3[j].Text = "<br/>";
                temp = " ";
                j++;

            }
            con.Close();
          }
         }
        }
            catch (Exception ex)
            {
                Response.Write("Error occured: " + ex.Message.ToString());
            }
            finally
            {
                Id = string.Empty;
            }

            System.Threading.Thread.Sleep(2000);
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 02, 2020 11:52 PM
indradeo says:
public void btnSendMail_Click(object sender, System.EventArgs e)
        {
            
            string Id = string.Empty;
            DataTable dt = new DataTable();
            try
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    
                    Id = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);
 
                    CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                    string flag = cb.Checked ? "Y" : "N";
                    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

Your problem is with the Connection string name.

The actual connection string name is ConnectionString and you are passing as constr.

So the error.