ben mah
ben mah

Reputation: 7

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();
}

}

Upvotes: 0

Views: 1203

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 21795

You are missing sqlupdateCmd.ExecuteNonQuery() after your update command. Also, I don't understand Why you are alerting the same message again in else block of code? The other thing is, probably UnitAvailable is a single value so instead of using ExecuteReader use 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.

Your Javascript alert shoul look like:-

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

Update 2:

Please use ExecuteScalar as i mentioned above like this:-

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();
}

Upvotes: 1

Banusundar Arumugam
Banusundar Arumugam

Reputation: 251

I hope u have to make two changes to the code.

  1. U should add ExecuteNonQuery after the update statement.
  2. Also the variable unitavailable1, which is the new available unit had been provided within the quotes..so probably the value is not substituted. 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".

Try this...Hope this will work...

Upvotes: 0

Related Questions