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

Bechir BejaouiRank #144 · 15.9K reputationPosted Nov 1, 2008, 1:10 PM EDT
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
Aston10 reputationPosted Nov 1, 2008, 6:26 PM EDT
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
Aston10 reputationPosted Nov 1, 2008, 7:02 PM EDT
http://bytes.com/forum/thread443767.html
Thanks for your assistance
Bechir BejaouiRank #144 · 15.9K reputationPosted Nov 4, 2008, 2:49 PM EST
Bechir BejaouiRank #144 · 15.9K reputationPosted Nov 4, 2008, 2:54 PM EST
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
Aston10 reputationPosted Nov 5, 2008, 8:59 PM EST
Dallr
Bechir BejaouiRank #144 · 15.9K reputationPosted Nov 13, 2008, 12:36 PM EST
charles2 reputationPosted Feb 8, 2009, 9:04 AM EST
Jan MontanoRank #1,295 · 1.3K reputationPosted Feb 8, 2009, 7:52 PM EST
{
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