简体   繁体   中英

Checking database data from textbox

I have a textbox which will take the input from user and search whether the inserted data is available in SQL database table or not. If the data is in table then it will update two column(time_out and day_out) of the same row.
Or else it will show an error message. This code below is not working. Please help.

try
{

    SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
    con3.Open();
    SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
    cmd3.Parameters.AddWithValue("@id", 1);
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now);

    int o = cmd3.ExecuteNonQuery();
    MessageBox.Show("Good Bye!");
    this.Close();
    FormCheck f2 = new FormCheck();
    f2.Show();
}
catch
{
    MessageBox.Show("Error!");
    textBox_VIex.Clear();
}

Please refer to my changes to your code,

      int o = cmd3.ExecuteNonQuery();

Returns the count of number of rows affected by the Query. If it is zero it means that id was not in the database.

  try
   {
     SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
     con3.Open();
     SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
    cmd3.Parameters.AddWithValue("@id", int.Parse(textBox_VIex.Text));
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now);

    int o = cmd3.ExecuteNonQuery();
    if(o > 0)
       MessageBox.Show("Good Bye!");
    else
      MessageBox.Show("Error!");
      this.Close();
      FormCheck f2 = new FormCheck();
      f2.Show();
   }
   catch
   {
    MessageBox.Show("Error!");
    textBox_VIex.Clear();
   }

Look into comments in code. First, this is good way to deal with connection, etc

int? result = null; // note nullable int
try
{
    Using (SqlConnection con = New SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True"))
    {
        con.Open();
        // You don't really care to check up front if the record with id=xx exists because
        // if it doesn't, you get 0 records updated
        // unless you do something with the result of that query
        Using (SqlCommandcmd As New SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@id", 1); // <<-- Are you always passing "1"?????
            cmd.Parameters.AddWithValue("@dO", DateTime.Now);
            cmd.Parameters.AddWithValue("@tO", DateTime.Now);        

            result = cmd.ExecuteNonQuery();
        }

        con.Close()
    } 
}
catch 
{
    // optionally, log error or show error msg in second msgBox below. Save it to a variable here
}

// Separate query and result processing

// Check the result. If result is still null, your query failed. If not 0 - you updated your records
if (result != null && (int)result > 0)
{  
    MessageBox.Show("Updated record OK");
}
else
{
    if (result == null)
    {  
        MessageBox.Show("The construct failed"); // optionally add error msg here
    }
    else
    {
       MessageBox.Show("The record I was looking for is not in DB"); 
    }
}      
// I don't know what you do with your form logic
this.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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