Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
sql = "select amount from storedData where barcode='" + txtBarcode.Text + "'";
cmd = new SqlCommand(sql, con);
if (rdbAdd.Checked == true)
{
    if (txtBarcode.Text.Length > 7)
    {
        txtBarcode.Text = txtBarcode.Text.Remove(0, 7);
        txtBarcode.SelectionStart = txtBarcode.Text.Length;
    }
    if (txtBarcode.Text.Length == 7)
    {
        amount = 0;
        con.Open();
        reader = cmd.ExecuteReader();
        try
        {
            reader.Read();
            if (reader.Read() == true)
            {
                amount = (int)reader["Amount"];
                amount++;
                sql = "update storedData set amount='" + amount + "' where barcode='" + txtBarcode.Text + "'";
                cmd.ExecuteNonQuery();
            }
            else
            {
                MessageBox.Show("new barcode add first");
            }
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        con.Close();
    }
}



i entered data in database each time i check it's there or not it says not y?
Posted
Updated 11-May-11 1:02am
v2
Comments
Wayne Gaylard 11-May-11 7:02am    
Just removed Ignore HTML tag
Mohammed Ahmed Gouda 11-May-11 7:04am    
okay :)
Sergey Alexandrovich Kryukov 11-May-11 13:43pm    
Tag SQL! Don't distract people with incomplete tags!
--SA

You are changing variable sql to a new SQL statement. But you do not change SqlCommand.CommandText ?


C#
if (rdbAdd.Checked == true)
{
    if (txtBarcode.Text.Length > 7)
    {
        txtBarcode.Text = txtBarcode.Text.Remove(0, 7);
        txtBarcode.SelectionStart = txtBarcode.Text.Length;
    }
    if (txtBarcode.Text.Length == 7)
    {
        sql = "select amount from storedData where barcode='" + txtBarcode.Text + "'";
        cmd = new SqlCommand(sql, con);

        amount = 0;
        con.Open();
        reader = cmd.ExecuteReader();
        try
        {
            reader.Read();
            if (reader.Read() == true)
            {
                amount = (int)reader["Amount"];
                amount++;
                sql = "update storedData set amount='" + amount + "' where barcode='" + txtBarcode.Text + "'";
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            else
            {
                MessageBox.Show("new barcode add first");
            }
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        con.Close();
    }
}


But it will proberly fail any way. As long you have are running a ExecuteReader(), you cannot use the same SqlConnection for other SQL statements.
 
Share this answer
 
v2
Comments
Mohammed Ahmed Gouda 11-May-11 7:22am    
??
Kim Togo 11-May-11 7:24am    
See updated answer.
Mohammed Ahmed Gouda 11-May-11 7:24am    
i changed it to sql2 do i have to make it cmd2 or not and it always gives me reader.read() false not true
Kim Togo 11-May-11 7:26am    
Create a SqlConnection, SqlCommand for reading, then create a new SqlConnection and SqlCommand for updating.
Mohammed Ahmed Gouda 11-May-11 7:27am    
okay but when i check if reader.read()==true it gives false and execute else statement
you are Using Select Query before Validating Your TextBox Value
so if you enter wrong BarCode it will not retrieve Data from the database

Use That Query Under your If Condtion /xml>
 
Share this answer
 
Comments
Mohammed Ahmed Gouda 11-May-11 7:12am    
same result

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