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();
}
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted May 8, 2014, 8:30 AM
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.
IsraelPosted May 8, 2014, 7:59 AM
VulpesPosted May 8, 2014, 7:19 AM
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?
IsraelPosted May 8, 2014, 7:08 AM
{
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();
}
}
VulpesPosted May 7, 2014, 1:37 PM
IsraelPosted May 7, 2014, 1:31 PM
VulpesPosted May 7, 2014, 12:13 PM
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();
}
}