Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What problem is with this update statement? Any one help me

UpdateQueryString = "UPDATE tblLogin SET loginName= @name, Password= @password, EmpID= @tempEmId WHERE LoginID= @tempLogId";

please help me

What I have tried:

UpdateQueryString = "UPDATE tblLogin SET loginName= @name, Password= @password, EmpID= @tempEmId WHERE LoginID= @tempLogId";

Error: must declare scalar variable "@name".
Posted
Updated 20-Nov-16 9:30am

There's nothing wrong with the statement. You need to set up parameters for @name, @password, @tempEmId and @tempLogId, assign the appropriate values to them, and assign them to the command.
 
Share this answer
 
Comments
safwanshah 20-Nov-16 10:12am    
i have changed this statement as follows:
UpdateQueryString = "UPDATE tblLogin SET loginName ='" + UUname + "', Password = '" + UUpassword + "', EmpID = '" + tempEmId + "' WHERE LoginID = '" + tempLogId + "'";

This is working perfect.
Midi_Mick 20-Nov-16 10:16am    
That is very bad practice. You should be using your original statement, but add the parameters to the SqlCommand object.
You will have something like:

cmd.CommandText = UpdateQueryString;

You need to add:

cmd.Parameters.AddWithValue("@name", UUname);
cmd.Parameters.AddWithValue("@password", UUpassword);

...and so on
safwanshah 20-Nov-16 10:29am    
UpdateQueryString = "UPDATE tblLogin SET loginName= @name, Password= @password, EmpID= @tempEmId WHERE LoginID= @tempLogId";

UpdateCommand.CommandText = UpdateQueryString;

UpdateCommand.Parameters.AddWithValue("@name", UUname);
UpdateCommand.Parameters.AddWithValue("@password", UUpassword);
UpdateCommand.Parameters.AddWithValue("@EmID", tempEmId);

UpdateCommand = new SqlCommand(UpdateQueryString, cc.logcon);

error displayed "Object reference not set to an instance of an object.
Midi_Mick 20-Nov-16 10:36am    
Put
UpdateCommand = new SqlCommand(....
BEFORE you add the parameters. Note, you are setting the CommandText in the Constructor, so you don't need
UpdateCommand.CommandText = UpdateQueryString;
line.
safwanshah 20-Nov-16 12:02pm    
cc.logcon.Close();
}
cc.logcon.Open(); //logcon.Open();

// UpdateQueryString = "UPDATE tblLogin SET loginName ='" + UUname + "', Password = '" + UUpassword + "', EmpID = '" + tempEmId + "' WHERE LoginID = '" + tempLogId + "'";
UpdateQueryString = "UPDATE tblLogin SET loginName= @name, Password= @password, EmpID= @tempEmId WHERE LoginID= @tempLogId";


UpdateCommand = new SqlCommand(UpdateQueryString, cc.logcon);
UpdateCommand.CommandText = UpdateQueryString;
UpdateCommand.Parameters.AddWithValue("@name", UUname);
UpdateCommand.Parameters.AddWithValue("@password", UUpassword);
UpdateCommand.Parameters.AddWithValue("@EmID", tempEmId);

SelectQueryString = "Select * from tblLogin where LoginID='" + tempLogId + "'";

SelectCommand = new SqlCommand(SelectQueryString, cc.logcon);
LoginReader = SelectCommand.ExecuteReader();

if (LoginReader.HasRows)
{
LoginReader.Close();


UpdateReader = UpdateCommand.ExecuteReader();
MessageBox.Show("Record updated");

UpdateReader.Close();

cc.logcon.Close();
return "1";
}
else
{
UpdateReader.Close();

cc.logcon.Close();
MessageBox.Show("Record not updated");
return "0";
}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
return null;

}
still encountered an error "must declare the scalar variable "tempEmId".
yeah last parameter added is wrong

C#
UpdateCommand.Parameters.AddWithValue("@EmID", tempEmId);



It should be

C#
UpdateCommand.Parameters.AddWithValue("@tempEmId", tempEmId);


and one more

C#
UpdateCommand.Parameters.AddWithValue("@tempLogId", tempLogId);


Carefully check the spellings and no. of parameters required. You only need four parameters

@name
@password
@tempEmId
@tempLogId
 
Share this answer
 
Comments
Midi_Mick 20-Nov-16 22:46pm    
Thanks for that Mohtshm - I'd gone to bed by the time he replied.
safwanshah 25-Nov-16 12:26pm    
Thanks Mohtshm Zubair. It's working perfect. I am very grateful to you and looking forward to a continuous support, please
safwanshah 25-Nov-16 12:26pm    
Thanks Mohtshm Zubair. It's working perfect. I am very grateful to you and looking forward to a continuous support, please
safwanshah 25-Nov-16 12:30pm    
Thanks a lot Mohtshm Zubair

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