C# SQL express Login
-
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