473,224 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 8024
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
5
by: ????? | last post by:
I have an access query which gets data from a number of different tables. Although the tables have primary key fields, the order in which the records are returned means that none of these are in...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
6
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor...
3
by: Kevin M | last post by:
I have one table and have created a form from that table. Also, I have created a delete query. I want to but a button on the form which will delete all records from the table; however, I cannot get...
14
by: Rich P | last post by:
Hi Sara, If you try it this way - you may have better luck Dim strSql As String strSql = "INSERT INTO tblPOCancels ( PONum, PODate, DateEntered, MerchantKey,VendorKey, POApproved, " _ &...
11
beacon
by: beacon | last post by:
Hi everybody, I created a database that links one table from an ODBC data source. I saved my password and UID to the data source so neither myself nor anyone else would have to login each time...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.