6个文本框的更新语句 [英] Update statement for 6 textboxes

查看:79
本文介绍了6个文本框的更新语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我已经在ASP中创建了一个表单并创建了一个数据库.每件事都可以,但是当我更新它时,例如如果我更新任何一个文本框并将其余部分留空,那么它将被更新为数据库中的空白字段,但是我只想更新为该字段.它显示异常错误为必须声明标量变量" @firstname.""
请帮助我

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

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",字符串).Value(Tbfname.Text);当我添加此显示无效的表达式字符串


sd.UpdateCommand.Parameters.Add("@firstName", string).Value(Tbfname.Text); WHEN I ADD THIS IT SHOWING INVALID expression string

推荐答案

检查文本框的值
check the value of text boxes
if(!string.IsNullOrEmpty(Tbfname.Text))
{
 sd.UpdateCommand.Parameters.AddWithValue("@firstname", Tbfname.Text);
}


为每个文本框执行此操作!


do this for each text box!


哦,太好了!绝对没有任何验证,最重要的是,您让用户输入ID值,这意味着ANY USER可以更改系统中ANY用户帐户的帐户详细信息(AND PASSWORD)!

我可以看到您的美好时光!
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!


您可以按以下方式创建查询



you can create your query as follows



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.


这篇关于6个文本框的更新语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆