简体   繁体   English

将文本框的数据与数据库进行比较,并使用新值更新数据库

[英]comparing data of textbox with database and update database with the new value

i am trying to compare the value of textbox with the data in database, and if the value in textbox is bigger den the value of data in database, a alert message will appear and if it is smaller than the value of the data in the datebase, it will minus the database value with the value of textbox and update the value of database. 我正在尝试将文本框的值与数据库中的数据进行比较,并且如果文本框中的值较大而数据库中的数据值较大,则会显示一条警告消息,并且如果它小于日期数据库中的数据值,它将减去数据库值和文本框的值,并更新数据库的值。 there is no error but when i click on the button, nothing appear and no changes have made to the database. 没有错误,但是当我单击按钮时,什么也没有出现,并且对数据库没有任何更改。 is there anything wrong with my code? 我的代码有什么问题吗? thx 谢谢

protected void btn_buy_Click(object sender, EventArgs e)
{


    hookUp = new SqlConnection("Server=localhost\\SqlExpress;Database=VoteNow;" +
    "Integrated Security=True" );
    sqlCmd = new SqlCommand("SELECT  UnitAvailable  FROM stockdetails1 WHERE StockID = 3", hookUp);
    hookUp.Open();
    reader = sqlCmd.ExecuteReader();
    int amountkey;
    amountkey = Convert.ToInt32(amount.Text);
    while (reader.Read())
    {
        int unitavailable = reader.GetInt32(0);
        int unitavailable1;
        if (amountkey <= unitavailable)
        {
           unitavailable1 = unitavailable - amountkey;
            SqlCommand sqlupdateCmd = new SqlCommand("UPDATE StockDetails Set UnitAvailable = '+unitavailable1+' WHERE StockID = 3", hookUp);

            sqlupdateCmd.ExecuteNonQuery();

            Response.Write("<script LANGUAGE='JavaScript'> alert('The units available is enough.')</script>"); 
        }
        else 
        {
            Response.Write("<script LANGUAGE='JavaScript'> alert('The units available is not enough.')</script>");

        }
    }
    reader.Close();
    hookUp.Close();
}

} }

You are missing sqlupdateCmd.ExecuteNonQuery() after your update command. 更新命令后缺少sqlupdateCmd.ExecuteNonQuery() Also, I don't understand Why you are alerting the same message again in else block of code? 另外,我不明白为什么您要在else代码块中再次提醒相同的消息? The other thing is, probably UnitAvailable is a single value so instead of using ExecuteReader use ExecuteScalar . 另一件事是, UnitAvailable可能是单个值,所以不要使用ExecuteReader而是使用ExecuteScalar

Also, consider implementing your code with using which will dispose the expensive resources such as connection and command objects.Please check this for more. 另外,还要考虑与执行你的代码using ,这将处置昂贵的资源,如连接和命令objects.Please检查这个更多。

Your Javascript alert shoul look like:- 您的Javascript警报应如下所示:-

Response.Write("<script type=\"text/javascript\">alert('The units available is not enough.')</script>");

Update 2: 更新2:

Please use ExecuteScalar as i mentioned above like this:- 请像上面我所说的那样使用ExecuteScalar :-

sqlCmd = new SqlCommand("SELECT  UnitAvailable  FROM StockDetails WHERE StockID = 3", hookUp);
hookUp.Open();
int unitavailable = Convert.ToInt32(sqlCmd.ExecuteScalar());
int amountkey;
amountkey = Convert.ToInt32(amount.Text);
int unitavailable1;
if (amountkey <= unitavailable)
{
    Response.Write("<script LANGUAGE='JavaScript'> alert('The units available is not enough.')</script>");
}
else
{
     unitavailable1 = unitavailable - amountkey;
     SqlCommand sqlupdateCmd = new SqlCommand("UPDATE StockDetails Set UnitAvailable = @unitavailable1 WHERE StockID = 3", hookUp);
     sqlupdateCmd.Parameters.Add("@unitavailable1",SqlDbType.Int).Value = unitavailable1;
     sqlupdateCmd.ExecuteNonQuery();
}

I hope u have to make two changes to the code. 我希望您必须对代码进行两项更改。

  1. U should add ExecuteNonQuery after the update statement. U应该在更新语句之后添加ExecuteNonQuery。
  2. Also the variable unitavailable1, which is the new available unit had been provided within the quotes..so probably the value is not substituted. 另外,在引号中提供了变量unitavailable1,这是新的可用单位。因此,该值可能不会被替换。 Try to close the quotes before that and open it after the variable and use '+' to concatenate them like this 尝试在此之前关闭引号,然后在变量之后将其打开,然后使用“ +”将其连接起来

"UPDATE StockDetails Set UnitAvailable = "+unitavailable1+" WHERE StockID = 3". “更新StockDetails设置UnitAvailable =“ + unitavailable1 +” StockID = 3“。

Try this...Hope this will work... 试试这个...希望这会起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM