Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
here my code is
C#
protected void btndelete_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Delete Employee1  Where EmployeeId=@PEmployeeId", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@PEmployeeId", TextBox1.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblmessage.Text = "Record deleted";
        }
        catch (NullReferenceException ex)
        {
            lblmessage.Text = ex.Message;
        }
what is this error why i'am getting this and how to solve this please help me thanks.
Posted
Comments
Richard MacCutchan 10-Feb-14 8:56am    
Use your debugger to step through the code and check the value of each variable that you are trying to use. Are you sure that your connection variable con has been initialised properly?
Karthik_Mahalingam 10-Feb-14 8:58am    
remove the try catch and see in which line you are getting this error

Normally, you would try to read a connection string from Web.Config by using its name:

C#
<connectionStrings>
  <add name="LoggingDatabase" connectionString="Database=XXX;Data Source=XXX;Initial Catalog=XXX;Integrated Security=True"/>



You appear to be trying to retrieve a connection string by using the string itself as the name...which is why you get the error, it doesn't exist so it returns null.
 
Share this answer
 
v2
Comments
raxhemanth 12-Feb-14 8:13am    
you are right mr sambasivarao G thanks for your reply thank you so much
Make sure that con object is not null.
Initialize the SqlConnection to the con object before using this event
 
Share this answer
 
May be your database Connection is not established properly...
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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