2
Reply

Error in login form

qing ryder

qing ryder

Oct 20 2017 10:42 AM
242
am trying to create a login form using sql and when the application runs, it crashes at
  1. da.Fill(dt); 
    1. public partial class Form1 : Form  
    2.     {  
    3.         SqlConnection con = new SqlConnection();  
    4.         public Form1()  
    5.         {  
    6.             SqlConnection con = new SqlConnection();  
    7.             con.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Xavier\Documents\Data.mdf;Integrated Security=True";  
    8.   
    9.             InitializeComponent();  
    10.         }  
    11.         private void pictureBox4_Click(object sender, EventArgs e)  
    12.         {  
    13.             SqlConnection con = new SqlConnection();  
    14.             con.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Xavier\Documents\Data.mdf;Integrated Security=True";  
    15.             con.Open();  
    16.             string Username = textBox1.Text;  
    17.             string Password = textBox2.Text;  
    18.             SqlCommand cmd = new SqlCommand("select userid,password from login where Username='" + textBox1.Text + "'and Password='" + textBox2.Text + "'", con);  
    19.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
    20.             DataTable dt = new DataTable();  
    21.             da.Fill(dt);  
    22.             if (dt.Rows.Count > 0)  
    23.             {  
    24.                 this.Hide();  
    25.                 Form2 f2 = new Form2();  
    26.                 f2.Show();  
    27.             }  
    28.             else  
    29.             {  
    30.                 MessageBox.Show("Invalid Login Credentials!");  
    31.             }  
    32.         }  
    33.     }  


Answers (2)