Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In C# I have been using the System.Data.SqlClient

I am trying to figure out how to find a certain data and get something else from it. Like I want to find a piece of Data which has a certain name, and I want to get it's Id number and/or row number. I haven't found out how to do it. Need helps! I've looked into query strings and SQLCommand. I haven't found out how to do this. Like I want it to be able return something to me.
Posted

This is a great place to start: ASP.NET Data Access Tutorials[^]

You might take a look at the other material too:
ASP.NET Web Forms[^]

[Update]
Doing it like RaviRanjankr suggests works fine, but it will not allow you to take advantage of the databinding features of the platform. Going through the tutorials will allow you to get more done with less work.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
fjdiewornncalwe 7-Jul-11 15:57pm    
Absolutely what he needs. +5
Espen Harlinn 7-Jul-11 16:09pm    
Thank you, Marcus!
You can use the ADO.NET DataReader to retrieve a read-only, forward-only stream of data from a database. Results are returned as the query executes, and are stored in the network buffer on the client until you request them using the Read method of the SqlDataReader.

SqlConnection con = new SqlConnection();
con.ConnectionString = "Write your Connection String values";
con.Open();
SqlCommand cmd = new SqlCommand("Your query using Where Clause",con);
            SqlDataReader dr = cmd.ExecuteReader();
 if(dr.Read())
{
// if dr is able to read value or dr have more than 0 rows
var value = dr[index] // assign 
}


for more Details take a look at this Link-MSDN-[Retrieving Data Using the DataReader][^]
 
Share this answer
 
Comments
Member 8015046 7-Jul-11 16:55pm    
if I do something like

string findId = "SELECT * FROM listMessages WHERE listNames_id IN (" + listName_ID + ")";
SqlCommand findIDCMD = new SqlCommand(findId, connection);
SqlDataReader dr = findIDCMD.ExecuteReader();
if (dr.Read())
{
long ID = Convert.ToInt64(dr["id"]);
return ID;
}

will it give me the first one that comes up?
RaviRanjanKr 9-Jul-11 6:37am    
if you are using * in your Query then be confirm to use Index No in dr(SQLdatareader) like 0,1,2 its depend upon your Table structure long ID = Convert.ToInt64(dr[indexno]);
and yes! it will give the first come value.
You can retrieve data from database using connected or disconnected architecture.
u need to modify ur select query.
this link can be useful to u
------------------------------------------------------------------------
http://amul-aspnetsampleprojects.blogspot.com/2011/07/deleteupdate-data-disconnected.html[^]

-------------------------------------------------------------------------

http://amul-aspnetsampleprojects.blogspot.com[^]
 
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