Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Im a newbie and just wanted to know how to retrieve a certain data from database ,its like i have my database filled with data such as id,name,class.
Example: name=kevin id=123 class=5
name=cris id=156 class=6
i want to retrieve one row of the table by using name or id.if i give the id or name and i should get the full data row
Thankyou in advance :)
Posted

C#
SqlConnection con = new SqlConnection("DatabaseConnectionString");

con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Student WHERE ID = " + id, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

foreach(DataRow dr in dt.Rows)
{
    idLbl.Text = dr["Id"].ToString();
    nameLbl.Text = dr["Name"].ToString();
    classLbl.Text = dr["ClassName"].ToString();
}


And you may find different tutorial for SQL and C#
Beginners guide to accessing SQL Server through C#[^]
http://msdn.microsoft.com/en-us/library/dw70f090(v=vs.110).aspx[^]

I hope this is what you're looking for..

-KR
 
Share this answer
 
The id will be the unique key to identify each row in a gridview, you can use DataKeyNames property of gridview to store the ids without displaying them. Refer: DataKeyNames[^]
 
Share this answer
 
v2

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