how to read values from database for loginpage(windows forms)?

Posted by Hariinakoti under C# on 4/11/2012 | Points: 10 | Views : 8982 | Status : [Member] | Replies : 21
Hi,
I created login page and registration page also.Now i want to take username and password for login page from registration page.
How to check that values from database?
any body reply me.
I'm poor in english plz understand my problem and reply asap..

Thanks & Regards
Hari

Thanks & Regards
Hari



Responses

Posted by: Rajni.Shekhar on: 4/11/2012 [Member] Bronze | Points: 25

Up
0
Down
You want to take user name and password from data base or from registration page? Please be clear.

Thanks,
Rajni Shekhar

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Rajni Shekhar ,
i'm saving registration page values in database.Now i want to take from database.If compare both values after that next page will be open.

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
Wat happen Rajni Shekhar?

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 4/11/2012 [Member] Bronze | Points: 25

Up
0
Down
Which database you are using like sql server or oracle?

Thanks,
Rajni Shekhar

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
Sqlserver 2008 only.

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 4/11/2012 [Member] Bronze | Points: 25

Up
0
Down
check following code

using System.Data;

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection("server=localhost;Database=MyDB;UserID=sa, PWD=1234;");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText="select count(*) from tabUser where userID='" + txtUserID.Text.Trim() + "' and password='" + txtPwd.Text.Trim() + "' ";
object iRecordCount = cmd.ExecuteScalar();
conn.Close();
if (Convert.ToInt16(iRecordCount) > 0)
{
Response.Redirect("next page url here");
}
else
{
//give message to user
}


Thanks,
Rajni Shekhar

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
ok thanq.But it is useful for windows form?

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 4/11/2012 [Member] Bronze | Points: 25

Up
0
Down
no, it will work for web site/web application also.

Thanks,
Rajni Shekhar

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Leila on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
You can use the same code published by Rajni Shekhar but instead of redirecting to a page, open the windows form you need.

Thanks,
Leila

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
ya, i need windows form

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jmckamal on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
As per your question and the thread going on here.. My assumption is that,

you have a registration page which is saving values in to the database.

Also You have one more page (Login Page) which is used to find - credentails passed by user is valid or not by checking from database.

Now you want to achieve this functionality by using windows forms.

If my understanding is correct .. Please find the below code:



Public Function validateName(txtuserName As String,txtpassword as string) As Integer
Using conn As New SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Staff;Integrated Security=True")
Using dbCommand As New SqlCommand("SELECT COUNT(*) FROM StaffDirectory WHERE StaffName=@Name and password=@pwd")
dbCommand.Connection = conn
dbCommand.Parameters.AddWithValue("@Name", txtuserName)
dbCommand.Parameters.AddWithValue("@pwd", txtpassword)
Dim matchesCount = dbCommand.ExecuteScalar
If matchesCount IsNot Nothing AndAlso matchesCount IsNot DBNull.Value Then
Return CInt(matchesCount)
End If
conn.Open()
End Using
End Using
End Function

-----

If the above code is perfectly working..
then you have to insert the below code to redirect to your homepage in the necessary place.

HomeForm objHomeForm = new HomeForm();
objHomeForm.Show();

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/11/2012 [Member] Starter | Points: 25

Up
0
Down
Thanq.But i want code in c#.

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/11/2012 [Member] Silver | Points: 25

Up
0
Down
hi hari,

free converting (vb to c#) tool in the following link...

http://www.developerfusion.com/tools/convert/vb-to-csharp/

Regards,
Singaravel M

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/11/2012 [Member] Silver | Points: 25

Up
0
Down
C# Code:


public int validateName(string txtuserName, string txtpassword)
{
using (SqlConnection conn = new SqlConnection("Data Source=.\\\\SQLEXPRESS;Initial Catalog=Staff;Integrated Security=True")) {
using (SqlCommand dbCommand = new SqlCommand("SELECT COUNT(*) FROM StaffDirectory WHERE StaffName=@Name and password=@pwd"))
{
dbCommand.Connection = conn;
dbCommand.Parameters.AddWithValue("@Name", txtuserName);
dbCommand.Parameters.AddWithValue("@pwd", txtpassword);
dynamic matchesCount = dbCommand.ExecuteScalar;
if (matchesCount != null && !object.ReferenceEquals(matchesCount, DBNull.Value))
{
return Convert.ToInt32(matchesCount);
}
conn.Open();
}
}
}



Regards,
Singaravel M

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/12/2012 [Member] Starter | Points: 25

Up
0
Down
Hi sakthi i know.
But i got error at dynamic matchesCount = dbCommand.ExecuteScalar; .So i asked ok.

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
can i know error details...?

Regards,
Singaravel M

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/12/2012 [Member] Starter | Points: 25

Up
0
Down
i got solution.
Thank you friends..

Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
may i know what is the error in above code...?

if u tell this means its used for all...

Regards,
Singaravel M

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi refer

http://www.dotnetfunda.com/codes/code2173-limitation-login.aspx

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 3/10/2013 [Member] Starter | Points: 25

Up
0
Down
I want to retrieve two values and store in a two variables how can i read two vales in a database?

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Mandlaa try this code

protected void bt1_Click(object sender, EventArgs e)
{
Dt = ReturnDt("Select StudentId,StudentName from dbo.Table_1"); // If U need 2 Values from Database and Retrieve Datatable
Dt = ReturnDt("Select * from dbo.Table_1");// If u need More than values from Database and Retrieve Datatable
}


public DataTable ReturnDt(string RecStr)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = RecStr;
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(Dt);
return Dt;
}

Mark as Answer if its helpful to you

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response