471,325 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

ASP.NET 2.0 SQL Query via C# Code

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
Jan 17 '06 #1
3 7990
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brett Baisley" wrote:
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

Jan 17 '06 #2
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!

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:19**********************************@microsof t.com...
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Brett Baisley" wrote:
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

Jan 17 '06 #3
On Mon, 16 Jan 2006 20:42:16 -0400, "Brett Baisley"
<ba*****@hotmail.com.REM> wrote:
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.

[snip]

You can do all of the "old" things in .NET 2.0 that you did in 1.1.
The DataAdapters and DataSets are still alive and well...

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Jan 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Harvey | last post: by
5 posts views Thread by ????? | last post: by
3 posts views Thread by cover | last post: by
6 posts views Thread by tizmagik | last post: by
3 posts views Thread by Kevin M | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.