Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sorry with my english and new to this
im trying to create a login form application with datatable in visualstudio

it works fine in my pc that i created the program with..
but after i create a setup for my laptop it gets me an error after i press login button

pop up window says

Login
X Unhandled exception has occured in your application..-

An attemp to attach an auto-named database for file C:\users\user\Documents\testlogin.mdf failed. A database with the same name exist,
or specified file cannot be opened, or it is located on UNC share.

What I have tried:

Sorry for language

LoginButton
private void btnLogin_Click(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\iSunner\Documents\testlogin.mdf;Integrated Security=True;Connect Timeout=30");
           SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where username ='" + TxtUsername.Text + "' and password='" + txtPassword.Text + "'", conn);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           if (dt.Rows[0][0].ToString() == "1")
           {
               this.Hide();
               ListMeja lm = new ListMeja();
               lm.Show();
           }
           else
           {
               MessageBox.Show("Username/password salah", "ADUH!", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }

       }
Posted
Updated 18-Aug-18 19:11pm

String Query="Select Count(*)from LogInfo where UserName=@uname and PassWord=@pwd;
SqlCoomad cmd=new SqlCoomand(Query,conn);
cmd.Parameter.Add(new SqlParameter("@Uname",Textbox1.Text));
cmd.Parameter.Add(new SqlParameter("@pwd",Textbox2.Text);
 
Share this answer
 
Comments
Muhammad nur Ihsan 19-Aug-18 7:58am    
im still getting error can you help to fix?

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\testlogin.mdf;Integrated Security=True;Connect Timeout=30");
String Query = "Select Count(*)from LogInfo where UserName='@Uname" + TxtUsername.Text + "' and password='@pwd" + txtPassword.Text + "'";
SqlCommand cmd = new SqlCommand(Query, conn);
cmd.Parameters.Add(new SqlParameter("@Uname", TxtUsername.Text));
cmd.Parameters.Add(new SqlParameter("@pwd", txtPassword.Text));
DataTable dt = new DataTable();
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
ListMeja lm = new ListMeja();
lm.Show();
}
C#
SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where username ='" + TxtUsername.Text + "' and password='" + txtPassword.Text + "'", conn);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Muhammad nur Ihsan 18-Aug-18 22:20pm    
so.. any idea which the best way to make a login form? im new to this sry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900