Hello!
My codes are working well without problem. What I need? I would like to have a warning message when the code was not find. Where can I put it and how to write thse codes.
private void btnfindcod_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 * from company where code ='" + txtcode.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();
while (read.Read())
{
txtprodut.Text = read["product"].ToString();
}
// It's probably here or not? ... code not found...
sqlCon.Close();
}
VulpesPosted Jul 5, 2014, 6:45 AM
private void btnfindcod_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=localhost\sqlexpress;InitialCatalog=master;Integrated Security=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select * from company where code ='" + txtcode.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();
if(read.HasRows)
{
while (read.Read())
{
txtprodut.Text = read["product"].ToString();
}
}
else
{
MessageBox.Show("A record with a code of " + txtcode.Text + " was not found");
}
read.Close();
sqlCon.Close();
}
Mfwamba TshimangaPosted Jul 5, 2014, 9:49 AM
VulpesPosted Jul 5, 2014, 9:19 AM
However, it looks like I've inadvertently lost the space between Initial Catalog which should, of course, be two separate words.
Mfwamba TshimangaPosted Jul 5, 2014, 9:03 AM
private void btnfindcod_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=localhost\sqlexpress;InitialCatalog=master;Integrated Security=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select * from company where code ='" + txtcode.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();
if(read.HasRows)
{
while (read.Read())
{
txtprodut.Text = read["product"].ToString();
}
}
else
{
MessageBox.Show("A record with a code of " + txtcode.Text + " was not found");
}
read.Close();
sqlCon.Close();
}