C # login page. SQL Server "sda.Fill (dt);" MISTAKE

I watched this video https://www.youtube.com/watch?v=tcmmCcMs8yU . He mainly teaches me how to create my own login page using SQL Server.

So, after he did exactly what he did, when I click the submit button, I get a highlight error sda.Fill(dt);

. I'm new to SQL Server, please advise!

My code:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Glenntdy\Documents\GlennTeoDB.mdf;Integrated Security=True;Connect Timeout=30");        

SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Table where Username='"+txtName.Text + "' and Password = '" +txtPassword.Text + "'",con);

DataTable dt = new DataTable();


if (dt.Rows[0][0].ToString() == "1")

    Main ss = new Main();
    MessageBox.Show("Please double check your Username and password");


Error image:

enter image description here

I am unable to post an image due to insufficient reputation

Thanks in advance!

EDIT: The main problem was solved, thanks to everyone!

Hopefully I'm not breaking any rules, but does anyone know how to change the password text to * when typing instead of only showing the plain password? Only on any other login page? - The problem was solved by Sohaibi. Many thanks!

EDIT [2]: The question has been resolved!


source to share

2 answers

The table is a reserved keyword and must be surrounded by curly braces like [Table]

. Also, it is not recommended to build your query the way you do it due to SqlInjection. Learn more about SqlParameters . One more thing .. you must close the SqlConnection after use.



enter image description here       private void button1_Click (object sender, EventArgs e) {SqlConnection sqlcon = new SqlConnection (@ "Data SOURCE =. \ SQLEXPRESS; AttachDbFilename = C: \ Users \ itmaint \ source \ repos \ database \ database \ G-data .mdf; Integrated Se a. curity = True; connection timeout = 30; user instance = True "); string query = "Select * from login, where username = '" + textBox1.Text.Trim () + "' and password = '" + textBox2.Text.Trim () + "'"; SqlDataAdapter sda = new SqlDataAdapter (query, sqlcon); DataTable dt = new DataTable (); sda.Fill (dt); if (dt.Rows.Count == 1) {Form2 ss = new Form2 (); this.Hide (); ss.Show (); }



All Articles