Reputation: 11
I'm trying to make a log in form in c# with database i don't know what to do and it is the code the error said the SQLException was Unhandled on the part of sda.Fill(dt) here is the code
SqlConnection con = new SqlConnection(@"Data Source=.\LOUI;Initial Catalog=login_db;User ID=sa;Password=1029384756");
SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From login_tbl where username = '" + User_txt.Text + "'and password = '" +Pass_txt.Text+ "'",con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
adminpanel ap = new adminpanel();
ap.Show();
}
else
{
MessageBox.Show("Check Username or Password");
}
Upvotes: 1
Views: 364
Reputation: 19
try
{
SqlConnection con = new SqlConnection(@"Data Source=.\LOUI;InitialCatalog=login_db;User ID=sa;Password=1029384756");//problem is here
SqlDataAdapter sda = new SqlDataAdapter("Select Count (*) From login where name = '" + User_txt.Text + "'and pass = '" + Pass_txt.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
adminpanel ap = new adminpanel();
ap.Show();
}
else
{
MessageBox.Show("Check Username or Password");
}
}
catch (Exception z)
{
MessageBox.Show("Connection error");
}
Upvotes: 0
Reputation: 788
Replace sda.Fill(dt);
with
try
{
sda.Fill(dt);
}
catch (SQLException ex)
{
Console.WriteLine(ex.ToString());
}
and edit your question to include the new output.
Upvotes: 2