Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
How to compare textbox value with the sql database value in c#?

im a beginner n i have to make a project. i only know how to connect database with the c# project. i m not asking for the exact code, kindly tell me any tutorial, link or anything that can help me.

One thing more, i dont have sql :( i have downloaded many setups but they r not complete. if anyone know any site from where i can get, do tell me please.
Posted
Comments
Uday P.Singh 13-Jun-11 12:01pm    
are you using Visual studio 2008/2010 then Sql server comes with Visual Studio
Sweety Khan 13-Jun-11 12:05pm    
Really? i have just now installed VS 2010 n i have seen sql 2008 configuration manager with it but no management studio. kindly tell me a little bit mOre
Uday P.Singh 13-Jun-11 12:15pm    
You can download Microsoft SQL Server 2008 Management Studio Express from here: http://www.microsoft.com/download/en/details.aspx?id=7593
Sweety Khan 14-Jun-11 5:55am    
its setup gives an error that it is not a valid win32 application
Uday P.Singh 14-Jun-11 12:45pm    
I think you have downloaded the 64 bit version and you computer is 32 bit, so have to download this :SQLManagementStudio_x86_ENU.exe it is for 32 bit computer, and will work as desired.

I think this will guide you.....
 
Share this answer
 
Comments
codesharper 14-Jun-11 13:26pm    
good call, my 5
You can follow these steps:
1. using System.Data.SqlClient;

2.
C#
protected void Button1_Click(object sender, EventArgs e)
    {
string str = "Data Source=ABC-PC\\SQLEXPRESS;Initial Catalog=demodb;Integrated Security=True";
            SqlConnection conn = new SqlConnection(str);
        conn.Open();
string adminId = txtadminId.Text;
string password = txtpassword.Text;
        string query = "SELECT admin_id, password FROM users WHERE id=@adminId
and password=@password";
        SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@admin_id", adminId);
cmd.Parameters.AddWithValue("@password", password);

SqlDataReader dr = cmd.ExecuteReader();



hope this helps:)
for more info comment here!!
 
Share this answer
 
Comments
Sweety Khan 14-Jun-11 16:19pm    
what is the purpose of these lines?
cmd.CommandType = CommandType.Text;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@admin_id", adminId);
Kim Togo 14-Jun-11 16:43pm    
It is called parameterized SQL, start using it :-)
Uday P.Singh 15-Jun-11 1:21am    
Yes! suggested by Kim these statement comes into play, when we use parametrized query, and it is always better to use it, to avoid sql-injection atttacks.
Sweety Khan 14-Jun-11 17:03pm    
i code this with the help of ur code n its working :)
string admin_id = A_TB1.Text;
string admin_password = A_TB2.Text;
SqlCommand c_admin_id = new SqlCommand("Select Admin_ID, Admin_Password from Administrator where Admin_ID = @admin_id and Admin_Password = @admin_password ", main_form);
c_admin_id.CommandType = CommandType.Text;
c_admin_id.Parameters.AddWithValue("@admin_id", admin_id);
c_admin_id.Parameters.AddWithValue("@admin_password", admin_password);
SqlDataReader r_admin_id = c_admin_id.ExecuteReader();
if (r_admin_id.HasRows)
{
Administrator a = new Administrator();
a.Show();
}
else
MessageBox.Show("ID or Password is invalid!");
Sweety Khan 14-Jun-11 17:05pm    
thanx a lOt! n be there i will pOst mOre questions :)
You can refer to the following links to get your job done:

http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx[^]

Using ADO.NET for beginners[^]

hope this helps :)
 
Share this answer
 
Comments
Sweety Khan 14-Jun-11 7:20am    
i have to compare user entered id n password with the database. so i specify sqlcommand n sqldatareader,tell me what to do next
SqlCommand c_admin_id = new SqlCommand("Select Admin_ID from Administrator ", main_form);
SqlDataReader r_admin_id = c_admin_id.ExecuteReader();
Sweety Khan 14-Jun-11 7:22am    
A_TB1.text = r_admin_id.?
A_TB1 is a textbox. can i compare its value like this? what should i use in palce of ?
Microsoft: http://www.microsoft.com/express/Database/[^] just follow the instructions. It's not a small download - couple hundred meg IIRC - but it is worth it.

To use it, try starting with w3schools[^] and/or look at teh articles here: Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
Comments
Sweety Khan 14-Jun-11 6:51am    
w3schools is best
sasdfcxzczx
 
Share this answer
 
Hi!!
I am kind of trying to do the same thing and getting stuck..Help please
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900