Click here to Skip to main content
15,883,940 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi programmers!

I know how to connect database and execute queries in ASP.Net. But I do not know on how to store the result of a query in a variable. Like for example, I would like to store the result of this query:
C#
SqlCommand cmd = new SqlCommand("SELECT Milestone_Code FROM tblMilestone WHERE MIlestone_Name='"+DropdownList1.SelectedItem+"'", amicassa);

in string res. How to do that?
PLEASE HELP ME!!!
Posted
Updated 20-Jul-14 20:11pm
v2

Hi,

SQL Clients Full ref : http://msdn.microsoft.com/en-us/library/system.data.sqlclient(v=vs.110).aspx[^]


SQL Command Details : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx[^]

2nd link will demonstrate how to fetch data and set it in a variable.

1st will help you to learn other prospective of SQL Client
 
Share this answer
 
Execute this query against a connection.
Then define a data reader and store results using this reader.
Refer
Retrieving Data Using a DataReader[^]
Retrieving Data Using a C# .NET DataReader [^]
 
Share this answer
 
v2
You can make use of ExecuteScalar if your query returns one value. For more than one, you can make use of ExecuteReader and store values from reader into your own object.

As a side note, will your code work? What does SelectedItem hold? You may have to change it to SelectedValue or SelectedText based on your needs.
 
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