4
Answers

object reference not set to an instance of an object c sharp

Shyja Abraham

Shyja Abraham

14y
13.3k
1
hai

i have a form with datagridview and a button,i need to insert the values of datagridview into the sql database when i click the button.

i use this code


                   connection cn;
                     SqlCommand cmd;

private void button1_Click(object sender, EventArgs e)
        {
            cn = null;
            cn = new connection();
            cmd = new SqlCommand();
            cmd.Connection = cn.connect();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
             {
            cn.insert(dataGridView1.Rows[i].Cells[0].Value.ToString(), dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Rows[i].Cells[2].Value.ToString(), textBox1.Text);
}
MessageBox.Show("datainserted");
}


in the class.cs


  public class connection
    {

        public SqlConnection con;
        public SqlCommand cmd;
        public SqlConnection connect()
        {
            con = new SqlConnection("server=HP-B67A56C6431D\\HORIZON;uid=sa;password=horizon;database=shyja;");
            
           
            con.Open();
            return con;
            
           
        }


        public void insert(string id, string name, string pho,string add)
        {
            cmd = new SqlCommand();
            cmd.Connection = connect();
            cmd.CommandText = "insert";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("id", SqlDbType.VarChar).Value = id;
            cmd.Parameters.Add("name", SqlDbType.VarChar).Value = name;
            cmd.Parameters.Add("pho", SqlDbType.VarChar).Value = pho;
            cmd.Parameters.Add("add", SqlDbType.VarChar).Value = add;
            cmd.ExecuteNonQuery();
        }

        

   }


but the error object reference not set to an instance of an object c sharp in the bolded line.plz help.


        
Answers (4)
0
Rahul Kaushik
NA 383 14.6k 7y
There is a perfect way while using flag to detect whether someone is already logged in or not and in case if System get power off , you need an event handler called as SystemEvents.SessionEnds.
 
Use this to change the flag value from true to false
 
Use this link as a reference
https://stackoverflow.com/questions/6799955/how-to-detect-windows-shutdown-or-logoff 
0
Mann Maurya
NA 47 867 7y
Hey Ajeesh
 
one more question... ??
 
If power off happen then how you suppose to Reset the flag
 
0
Ajeesh
NA 349 1.6k 7y
You can take a look at this article which discuss a similar topic. 
 
https://blog.learningtree.com/preventing-duplicate-logins-in-asp-net/
 
Another option would be to have a flag in your table and set the value to true when an user login and when user log off set the value to false.  Also for when a second user tries to login we will first check if login flag has been set to true for any other user. If its true then show message to user and display the login page, if not allow the user to login.