C# SQL express Login
-
i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EasyStorePro { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********"; string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'"); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("Welcome"); } else { MessageBox.Show("Invalid Entry"); } conn.Close(); } } }
Thank you for your time
-
i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EasyStorePro { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********"; string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'"); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("Welcome"); } else { MessageBox.Show("Invalid Entry"); } conn.Close(); } } }
Thank you for your time
How this is a database related question? :doh: BTW, you can show the Login form as dialog using Form_Name.ShowDialog(). It'll open up as a popup dialog.
-
How this is a database related question? :doh: BTW, you can show the Login form as dialog using Form_Name.ShowDialog(). It'll open up as a popup dialog.
I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.
-
I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.
ok, are you getting any error? Exactly what problem are you facing?
-
ok, are you getting any error? Exactly what problem are you facing?
when I load up the program and open the login form, I input the username and password no error pops up just the messagebox, that says invalid entry, but the information i entered is the exact same information I put into the database itself. i am thinking my problem lies within the if statement but i could be wrong
if (dt.Rows.Count > 0) { MessageBox.Show("Welcome"); } else { MessageBox.Show("Invalid Entry"); }
i should be getting the messagebox that states Welcome, but im not.
-
I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.
Try using this
string str = "select count(*)from Login where username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
conn.Open();
int rows = cmd.ExecuteScalar();
if (row > 0)
{
MessageBox.Show"Welcome");
}
else
{
MessageBox.Show("Invalid Entry");
}
conn.Close(); -
Try using this
string str = "select count(*)from Login where username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
conn.Open();
int rows = cmd.ExecuteScalar();
if (row > 0)
{
MessageBox.Show"Welcome");
}
else
{
MessageBox.Show("Invalid Entry");
}
conn.Close(); -
i get the following error when trying to run the program Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
Ofcourse you have to cast it to int, i missed it and you too.
int row = Convert.ToInt32(cmd.ExecuteScalar());
-
Ofcourse you have to cast it to int, i missed it and you too.
int row = Convert.ToInt32(cmd.ExecuteScalar());
lol yeah i just converted it then seen your reply, ok the program runs now with no errors but i still get the Invalid Entry MessageBox when inputting the username and password, i even added a new entry to the database and still get the same thing =/ .. this has had be stumped for the past few days
-
lol yeah i just converted it then seen your reply, ok the program runs now with no errors but i still get the Invalid Entry MessageBox when inputting the username and password, i even added a new entry to the database and still get the same thing =/ .. this has had be stumped for the past few days
:confused: Trace the program, see what is the value of "str" before executing the command.
-
:confused: Trace the program, see what is the value of "str" before executing the command.
I looked at it, the str is the string for SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'" i removed the string for this and put it directly into the cmd section...
System.Data.SqlClient.SqlCommand cmd; cmd = new System.Data.SqlClient.SqlCommand("SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'", conn);
still not having any luck getting it to work. thank you for time
-
i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EasyStorePro { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********"; string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'"); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("Welcome"); } else { MessageBox.Show("Invalid Entry"); } conn.Close(); } } }
Thank you for your time
Just set a break point after the SELECT query is constructed. When it breaks at that point, copy the value of the string out and paste it somewhere else to execute the query to see the result. You may figure out the problem that way.
-
I looked at it, the str is the string for SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'" i removed the string for this and put it directly into the cmd section...
System.Data.SqlClient.SqlCommand cmd; cmd = new System.Data.SqlClient.SqlCommand("SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'", conn);
still not having any luck getting it to work. thank you for time
I said you to see the value of "str" after tbUsername & mtbPassword is binding with it. Maybe something is wrong with those values.
-
Just set a break point after the SELECT query is constructed. When it breaks at that point, copy the value of the string out and paste it somewhere else to execute the query to see the result. You may figure out the problem that way.
I too think this is the way to go. Step 1: First you have to make sure the connection string is correct. Try this.
string str = ("SELECT username,password FROM dbo.Login "); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt);
then debug and see if the da has all details from the database table. Step 2: If step 1 was successful, use your program, debug and see what is the value of str just before the query is fired. Execute that query directly from Query Analyzer and verify the results. -
I too think this is the way to go. Step 1: First you have to make sure the connection string is correct. Try this.
string str = ("SELECT username,password FROM dbo.Login "); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt);
then debug and see if the da has all details from the database table. Step 2: If step 1 was successful, use your program, debug and see what is the value of str just before the query is fired. Execute that query directly from Query Analyzer and verify the results.ok with that i get an error due to no connection string so i add in conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=********;Password=***********"; and it runs i think my main problem is in the if statment section, more or less getting the textbox to verify itself with the information in the database so something like password is equal to mtbPassword.Text
-
i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EasyStorePro { public partial class LoginForm : Form { public LoginForm() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********"; string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'"); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("Welcome"); } else { MessageBox.Show("Invalid Entry"); } conn.Close(); } } }
Thank you for your time
can i get the corrected code plz i am working on the same project
-
I said you to see the value of "str" after tbUsername & mtbPassword is binding with it. Maybe something is wrong with those values.
can i get the corrected code plz i am working on the same project