ASP.NET 2.0 SQL Query via C# Code

B

Brett Baisley

Hello,

I am trying to write C# code (for an ASP.NET 2.0 page) that will query an
SQL database.
Every tutorial I find online always says "don't write code, drag and drop
the server controls", such as the dataSource and bind it to a control.

The problem is, I don't want to drag and drop, I want to do it via code. I
have a connection already made, and I want to, via code, run an SQL select
to get the next available ID number from a table and store it in a variable
for futher processing.

Can anyone point me to something that will show me how to do this? Do I need
to create a new datasource to execute my query?

Thanks,
Brett
 
G

Guest

Certainly you can do it without the "widgets".
1) create a SqlConnection
2) create a SqlCommand and assign the connection to it.
3) call the appropriate method of the SqlCommand object, supplying whatever
required parameters are necessary.
It "sounds" like you want ExecuteScalar, since you only want to get back a
single item. Provided the Sql Statement or stored proc is correct, that's
what you'll get.
Peter
 
B

Brett Baisley

Thanks Peter,

I was thinking it was something similiar to that, but I wasn't sure if there
was a "new" way to do it in version 2.0.

I'll give it a try. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top