473,499 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query from ASP page

I'm new at ASP and I'm trying to query an MS SQL database (Northwind). I
want the page to be setup with a textbox where the user inputs a list of
comma separated numbers and the query would be something like:

select * from categories where categoryid in ('1','2','3','4','5')

and the results would be in a datagrid, I think. This is easy in Query
analyzer but putting it on a web form seems very hard.(for me)

This doesn't seem possible, is it possible? suggestions ?
Nov 21 '05 #1
11 1509
It is possible.
Aare you using ASP or ASP.NET? In old ASP2.0 you will have to build you
datagrid as HTML. ASP.NET will do this for you.
Let me know what version of ASP are you using and I can help you with
some samples.

Nov 21 '05 #2
I'm using ASP.net in Visual Web Developer 2005 express edition btea 2.

code:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"
TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description"
HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName],
[Description] FROM [Categories] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="CategoryID"
PropertyName="Text"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

</div>
</form>

"alex_s" wrote:
It is possible.
Aare you using ASP or ASP.NET? In old ASP2.0 you will have to build you
datagrid as HTML. ASP.NET will do this for you.
Let me know what version of ASP are you using and I can help you with
some samples.

Nov 21 '05 #3
frogive me, I used the wizard...

"Daniel Seipel" wrote:
I'm using ASP.net in Visual Web Developer 2005 express edition btea 2.

code:
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"
TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
<asp:BoundField DataField="Description"
HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName],
[Description] FROM [Categories] WHERE ([CategoryID] = @CategoryID)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="CategoryID"
PropertyName="Text"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

</div>
</form>

"alex_s" wrote:
It is possible.
Aare you using ASP or ASP.NET? In old ASP2.0 you will have to build you
datagrid as HTML. ASP.NET will do this for you.
Let me know what version of ASP are you using and I can help you with
some samples.

Nov 21 '05 #4
Daniel,

You mean this?
\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection _
("Server=(Local); DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("select * from categories where categoryid in (" _
& TextBox1.Text & ")", conn)
Dim ds As New DataSet
da.Fill(ds, "categories")
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
End Sub
///
You can as well use a datareader instead of a dataadapter/dataset

I hope this helps,

Cor
Nov 21 '05 #5
looks good, it's been a while since I've done any vb.net. Let me make sure
this will work well though. What if the categories table contained ....over
a million rows? would this still work ok?

"Cor Ligthert [MVP]" wrote:
Daniel,

You mean this?
\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection _
("Server=(Local); DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("select * from categories where categoryid in (" _
& TextBox1.Text & ")", conn)
Dim ds As New DataSet
da.Fill(ds, "categories")
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
End Sub
///
You can as well use a datareader instead of a dataadapter/dataset

I hope this helps,

Cor

Nov 21 '05 #6
actually this pulled all of the categories from the category table, I would
like it be based on the textbox input, separated by commas.

"Cor Ligthert [MVP]" wrote:
Daniel,

You mean this?
\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection _
("Server=(Local); DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("select * from categories where categoryid in (" _
& TextBox1.Text & ")", conn)
Dim ds As New DataSet
da.Fill(ds, "categories")
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
End Sub
///
You can as well use a datareader instead of a dataadapter/dataset

I hope this helps,

Cor

Nov 21 '05 #7
Daniels,

Did you try it?

Cor
Nov 21 '05 #8
yes, I did but when I run the page, the query runs automatically with no
input and all i get is the results of 'select * from categories'.

"Cor Ligthert [MVP]" wrote:
Daniels,

Did you try it?

Cor

Nov 21 '05 #9
Daniel,

Strange I tested it, did you drag a textbox, a button and datagrid on your
page?

I expected that it was clear from the sample that it was needed.
Cor
Nov 21 '05 #10
Yes, I've done all that, are you using ASP.net in Visual Web Developer 2005
express edition beta 2?

I create a page, drag a textbox, button and gridview on the page. This is
ASP.NET 2.0 beta btw,
"Cor Ligthert [MVP]" wrote:
Daniel,

Strange I tested it, did you drag a textbox, a button and datagrid on your
page?

I expected that it was clear from the sample that it was needed.
Cor

Nov 21 '05 #11
Daniel,
I create a page, drag a textbox, button and gridview on the page. This is
ASP.NET 2.0 beta btw,

It is always nice when you give good information. This was your question.
and the results would be in a datagrid, I think. This is easy in Query
analyzer but putting it on a web form seems very hard.(for me)


However doing this in 2.0 gives the same result.

\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection _
("Server=(Local); DataBase=Northwind;" & _
"Integrated Security=SSPI")

TextBox1.Text = "'1','2','3'" 'Hardcoded to avoid mistyping in this
sample
'this above should be deleted for real use

Dim da As New SqlClient.SqlDataAdapter _
("select * from categories where categoryid in (" _
& TextBox1.Text & ")", conn)
Dim ds As New DataSet
da.Fill(ds, "categories")
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End Sub
////

I hope this helps,

Cor
Nov 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2631
by: Bill H | last post by:
I have a routine that displays 60 items (with thumbnails) per page. You can click any item and displays a new page with item details. When the user back pages it runs the query again to display all...
4
1579
by: knoak | last post by:
Hi there, I'm building a website about animals. There is a mySQL DB behind it. There are 2 tables: 1 - species 2 - animals Species could be: birds, fishes etc. Animals would be: hawk, eagle...
16
2707
by: cover | last post by:
I have a form with drop down menus to query for name, month, and year to capture activity accordingly by an individual for a given month and given year. But I'd like to also be able to query ALL...
3
5368
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...
3
7769
by: krystoffff | last post by:
Hi I would like to paginate the results of a query on several pages. So I use a query with a limit X offset Y to display X results on a page, ok. But for the first page, I need to run the...
3
2565
by: Steven J. Reed | last post by:
I must be missing something, because this should be easy to do. After the user has selected several options on a page, I do a database query which returns a few records that I want to pass to...
3
16385
by: racquetballguy | last post by:
Hi I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query. SELECT TOP does not support parameters. Something like...
1
3194
by: kurty | last post by:
Hello all, I am currently working on a project (a web based database) which entails the use of HTML, PHP and MySQL. I am new to all of these languages and would like some assistance in solving the...
3
2313
by: Mesut | last post by:
I have written a form in with radio buttons the name is set to orderby and the value is set to KundeVorName and the next value is KundeNachName and it goes so on. I wanna modify my query according...
2
1818
bugboy
by: bugboy | last post by:
Hi i'm a beginner at php and my simple web query page doesn't work.. i swear i had it working at one point but now i've come back to it it doesn't. • My form works • My DB connection works •...
0
7134
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
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
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...
0
5479
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
3105
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
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.