Hi!
How can I receive a message saying for example: "Code not found. Thanx!"
See what I wrote and its doenst result:
private void btnSearch_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select code, product, stocks from searching where code='" + code.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();
while (read.Read())
{
code.Text = read["code"].ToString();
product.Text = read["product"].ToString();
Stocks.Text = read["stocks"].ToString();
}
//problem start here
else
{
MessageBox.Show("Code not found. Thanx!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlCon.Close();
}

VulpesRank #55 · 29.2K reputationPosted May 7, 2014, 8:13 AM EDT
private void btnSearch_Click(object sender, EventArgs e)
{
SqlConnection sqlCon = null;
SqlDataReader read= null;
try
{
string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True";
sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select code, product, stocks from searching where code='" + code.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
read = sqlCmd.ExecuteReader();
while (read.Read())
{
code.Text = read["code"].ToString();
product.Text = read["product"].ToString();
Stocks.Text = read["stocks"].ToString();
}
}
catch(IndexOutOfRangeException)
{
MessageBox.Show("Code not found. Thanx!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
read.Close();
sqlCon.Close();
}
}
IsraelRank #1,223 · 1.5K reputationPosted May 7, 2014, 9:31 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 7, 2014, 9:37 AM EDT
IsraelRank #1,223 · 1.5K reputationPosted May 8, 2014, 3:08 AM EDT
{
SqlConnection sqlCon = null;
SqlDataReader read= null;
try
{
string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True";
//sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select code, product, stocks from searching where code='" + code.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
read = sqlCmd.ExecuteReader();
while (read.Read())
{
code.Text = read["code"].ToString();
product.Text = read["product"].ToString();
Stocks.Text = read["stocks"].ToString();
}
}
catch(IndexOutOfRangeException)
{
MessageBox.Show("Code not found. Thanx!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
read.Close();
sqlCon.Close();
}
}
VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 3:19 AM EDT
If it does, are you getting a null reference exception when you try to run it and are you able to pinpoint, using the debugger, on which line the exception occurs?
IsraelRank #1,223 · 1.5K reputationPosted May 8, 2014, 3:59 AM EDT
VulpesRank #55 · 29.2K reputationPosted May 8, 2014, 4:30 AM EDT
Just one point though. Is the 'code' column a numeric or char based column in the database?
If it's numeric, then you won't need the surrounding single quotes in the SELECT query.