Click here to Skip to main content
15,883,960 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                cmd.CommandText = "select count(*) from test;";
                cmd.Connection = con;
                int a = (int)cmd.ExecuteScalar();
                dr.Close();
                con.Close();
                string status = Convert.ToString(a);
                label1.Text = status;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                this.Close();
            }
        }
Posted
Comments
Mufid Abdul Gafoor Bagdadi 23-Sep-14 1:20am    
It shows an error "Object reference not set to an instance of an object."
Abdul Samad KP 23-Sep-14 1:22am    
What is dr.Close();
Where you are declaring your command and connection?
Have you initialized them?
Torakami 23-Sep-14 1:31am    
I think dr here is not required
[no name] 23-Sep-14 1:33am    
could you please show your connection class
Why you use dr.close ?
Mufid Abdul Gafoor Bagdadi 23-Sep-14 1:40am    
thnx all of you.

I think you miss the connection details

C#
string connectionString = "Connection String details";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
 
Share this answer
 
Better you follw this
//Connecion string .....
            SqlCommand cmd= new SqlCommand("select count(*) from test", con);
	    int noRows;
            con.Open();
	    noRows = cmd.ExecuteNonQuery();
	    con.Close();
	    label1.Text= Convert.ToString(noRows);
 
Share this answer
 
Hi,

You did not initiate your connection or command object.

Thanks...
 
Share this answer
 
Your code looks fine
Please check your connection and command objects whatever you defined outside.

See it's instantiating or not
C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                cmd.CommandText = "select count(*) from test;";
                cmd.Connection = con;
                int a = (int)cmd.ExecuteScalar();  
                con.Close();     
                label1.Text = a.ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                this.Close();
            }
        }
 
Share this answer
 
You have to create instance of your sqlconnection and sqlcommand with new operator.
No need to write dr.Close();

Please try this way. It works.
try
 {
     string connectionString = "Your connection string";
     SqlConnection con = new SqlConnection(connectionString);
     SqlCommand cmd = new SqlCommand();
     con.Open();
     cmd.CommandText = "select count(*) from test;";
     cmd.Connection = con;
     int a = (int)cmd.ExecuteScalar();
     con.Close();
     string status = Convert.ToString(a);
    // label1.Text = status;
 }
 catch (Exception err)
 {
     MessageBox.Show(err.Message);
     this.Close();
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900