Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I had created a form in ASP and created a database. Every thing is ok but when I am updating it, like if I am updating any one text box and left the rest blank then it will be updated as the fields blank in the database, but i want to be updated to only that field.Now it showing exceptional error as ""Must declare the scalar variable "@firstname".""
please help me
C#
protected void Btndisplay_Click(object sender, EventArgs e)
        {
            sd.UpdateCommand = new SqlCommand("update formtest set firstname=@firstname, username=@username, pass=@pass, email_id=@email_id where ID=@ID", conn);
            if (!string.IsNullOrEmpty(Tbfname.Text))
            {
                sd.UpdateCommand.Parameters.AddWithValue("@firstname", Tbfname.Text);
            }
            if (!string.IsNullOrEmpty(TbUname.Text))
            {
                sd.UpdateCommand.Parameters.AddWithValue("@username", TbUname.Text);
            }
            if (!string.IsNullOrEmpty(TbPass.Text))
            {
                sd.UpdateCommand.Parameters.AddWithValue("@pass", TbPass.Text);
            }
            if (!string.IsNullOrEmpty(Tbemail.Text))
            {
                sd.UpdateCommand.Parameters.AddWithValue("@email_id", Tbemail.Text);
            }
            if (!string.IsNullOrEmpty(Tbid.Text))
            {
                sd.UpdateCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(Tbid.Text));
            }
            conn.Open();
            sd.UpdateCommand.ExecuteNonQuery();
            conn.Close();
         }

sd.UpdateCommand.Parameters.Add("@firstName", string).Value(Tbfname.Text); WHEN I ADD THIS IT SHOWING INVALID expression string
Posted
Updated 3-Jun-12 7:44am
v4

check the value of text boxes
C#
if(!string.IsNullOrEmpty(Tbfname.Text))
{
 sd.UpdateCommand.Parameters.AddWithValue("@firstname", Tbfname.Text);
}

do this for each text box!
 
Share this answer
 
Comments
VJ Reddy 3-Jun-12 12:25pm    
Good answer. 5!
selflearning 3-Jun-12 12:30pm    
It showing exceptional error as "Must declare the scalar variable "@firstname"."
Ohh, nice! Absolutely no validation at all, and on top of that, you're letting the users input ID values, meaning that ANY USER can change the account details (AND PASSWORD) of ANY user account in your system!

I can see good times ahead for you!
 
Share this answer
 
Comments
selflearning 3-Jun-12 15:22pm    
Thanks for your reply.. its just a simple CRUD nothing else is over and coming to the validations how can you show that one in coding. I know I am having a very good time and thanks for your suggestion.
you can create your query as follows



C#
string sqlQuery="update formtest set ";
if(!string.IsNullOrEmpty(Tbfname.Text))
{
 sd.UpdateCommand.Parameters.AddWithValue("@firstname", Tbfname.Text);

sqlQuery = sqlQuery +" firstname=@firstname"
}


now use your logic to create your query with all textboxes. than in which textbox you enter data only which data will update rest will remain unupdated.
 
Share this answer
 
Comments
selflearning 5-Jun-12 7:27am    
Thanks for your valuable reply but still it giving the exception error...

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