473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8027
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.