Object reference not set to an instance of an object.

Posted by Sudheep.grandhe under C# on 12/17/2011 | Points: 10 | Views : 2855 | Status : [Member] | Replies : 3
Hi,

it shows the following error.Error:Object reference not set to an instance of an object.

public int ExecuteScalar(string Querystring)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand(Querystring, con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
return (int)cmd.ExecuteScalar();//error:Object reference not set to an instance of an object.
}
private void btnupdate_Click(object sender, EventArgs e)
{
string querystring = string.Empty;
if (!Validation())
return;
if (txtNewpassword.Text.Trim() != txtreenternewpassword.Text.Trim())
{
MessageBox.Show("Re-Entered Password does match with new password", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
querystring ="update tbl_login set username='" +txtusername .Text .Trim ()+"' where password='"+txtoldpassword .Text .Trim ()+"'";
int count= ExecuteScalar(querystring );
if (count > 0)
{
querystring ="update tbl_login set password='" +txtNewpassword .Text .Trim ()+"' where username='" +txtusername .Text .Trim ()+"'";
MessageBox .Show ("Password has been changed successfully","Information",MessageBoxButtons .OK ,MessageBoxIcon.Information );
}
}
}

Best,
Sudheep.



Responses

Posted by: Hmanjarawala on: 12/17/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

This happens because you ExecuteScalar() methods returns "null".

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/17/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Thanks for replying to me.how can i resolve this error.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/17/2011 [Member] Bronze | Points: 25

Up
0
Down
Let me see your Query

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response