Reputation: 65
I am writing a C# windows form, so when you click exit, it closes the
window, and when you click login, it checks the login info and takes you to
another form if it is correct. When i click login, i get an error that says,
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll. It throws the error on the line that says sda.Fill(dt);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\carme\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30;");
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Login where Username'" + textBox1.Text + "' and Password = '" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Main ss = new Main();
ss.Show();
}
else
{
MessageBox.Show("Check your username and password");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Upvotes: 1
Views: 698
Reputation: 4891
Maybe you should change
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Login where Username'" + textBox1.Text + "' and Password = '" + textBox2.Text + "'", con);
to
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Login where Username ='" + textBox1.Text + "' and Password = '" + textBox2.Text + "'", con);
Upvotes: 1
Reputation: 3469
You need to open your connection, then you should use using statements because SqlConnection
and SqlDataAdaptor
implement IDisposable
and should be closed. The using statements will take care of that for you.
Also use parameters to prevent SQL injection.
Also you forgot the '=' after username.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\carme\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30;"))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Login where Username = @userName and Password = @passWord, con))
{
sda.SelectCommand.Parameters.AddWithValue("@userName", textBox1.Text);
sda.SelectCommand.Parameters.AddWithValue("@passWord", textBox2.Text);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Main ss = new Main();
ss.Show();
}
else
{
MessageBox.Show("Check your username and password");
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I haven't tested it, but it looks like its right to me.
Upvotes: 0