I have been searching for this for over 2 hours now.
I have a c# application and I want upon successful login a main form is opened and login form is closed.
How is the best way to hand this. If I can get a code sample it will be greatly appreciated.
I am new to .net
Dallr
Loading
Jan MontanoPosted Feb 9, 2009, 12:52 AM
{
system.data.SqlClient.Sqlconnection oConn = new SqlConnection(UserInformationConnectionString);
system.data.SqlClient.Sqlcommand oComm = new SqlCommand();
oComm.CommandType = CommandType.StoredProcedure;
oComm.CommandText = "LOGIN";
SqlParameter idParam = new SqlParameter("@Username", string);
idParam.Value = idtextbox.Text;
SqlParameter PassParam = new SqlParameter("@Password", string);
idParam.Value = idtextbox.Text;
SqlCommand.Paramters.Add(idParam);
SqlCommand.Paramters.Add(PassParam);
try
{
oConn.Open();
int indice = oComm.ExecuteNonQuery();
if(indice!=0) Form1.Show();
if(indice==0)MessageBox.Show("Login failed");
}
finally
{
oConn.Close();
}
}
This is the problem with the code:
SqlParameter idParam = new SqlParameter("@Username", string);
idParam.Value = idtextbox.Text;
SqlParameter PassParam = new SqlParameter("@Password", string);
idParam.Value = idtextbox.Text;
you passed string as an argument in the SqlParameter constructor instead of putting the value of the field.
It should be:
SqlParameter idParam = new SqlParameter("@Username", idtextbox.Text);
//idParam.Value = idtextbox.Text;
SqlParameter PassParam = new SqlParameter("@Password", idtextbox.Text);
//idParam.Value = idtextbox.Text;
note: for the PassParam, I think you should have something like passtextbox.Text as argument
charlesPosted Feb 8, 2009, 2:04 PM
Bechir BejaouiPosted Nov 13, 2008, 5:36 PM
AstonPosted Nov 6, 2008, 1:59 AM
Dallr
Bechir BejaouiPosted Nov 4, 2008, 7:54 PM
But now it's pretty ok with the .Net, but never the less you see this piece of code It could be very useful for you you know? even with a web context but with some rectifications
By the way, I invite you to take a look on my blog listed bellow the blogspot one, there are many useful code there, in addition, I writted a sort of revision of the .Net framework very useful for someone who wants to understand the .Net framework and pass the certification exam
Bechir BejaouiPosted Nov 4, 2008, 7:49 PM
AstonPosted Nov 1, 2008, 11:02 PM
http://bytes.com/forum/thread443767.html
Thanks for your assistance
AstonPosted Nov 1, 2008, 10:26 PM
From you code it looks like you are doing this via this line if(indice!=0) yourMainForm.Show();.
I am not getting the method myMainForm.Show() when i use the above.
Regards,
Dane
Bechir BejaouiPosted Nov 1, 2008, 5:10 PM
So create a procedure that verify the existance of the user
CREATE PROCEDURE LOGIN
(
@UserID as nvarchar(50),
@Password as nvarchar(50)
)
SELECT User,Password FROM User WHERE User=@UserID AND Password = @Password
GO
then create a login page with two text boxes and two button login and cancel
implement the code of the login button as follow
public void login_click(.......)
{
system.data.SqlClient.Sqlconnection oConn = new SqlConnection(/*your connection string here*/);
system.data.SqlClient.Sqlcommand oComm = new SqlCommand();
oComm.CommandType = CommandType.StoredProcedure;
oComm.CommandText = "LOGIN";
SqlParameter idParam = new SqlParameter("@UserID", string);
idParam.Value = idtextbox.Text;
SqlParameter PassParam = new SqlParameter("@Password", string);
idParam.Value = idtextbox.Text;
SqlCommand.Paramters.Add(idParam);
SqlCommand.Paramters.Add(PassParam);
try
{
oConn.Open();
int indice = oComm.ExecuteNonQuery();
if(indice!=0) yourMainForm.Show();
if(indice==0)MessageBox.Show("Login failed");
}
finally
{
oConn.Close();
}
}
implement the Cancel button
public void Cancel_Click(...)
{
Application.Exit();
}
go to the program.cs and set Application.Run(yourLoginPage...) instead of the default form1 page