473,322 Members | 1,540 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,322 software developers and data experts.

Reader parameters



I have used the following to get data from my SQL database and populate text
& label boxes.

I understand how to use parameters with gridview and data view but not with
the reader.

How can I change this code to use parameters and so give some protection
from insertion issues.

Dim con As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("mydataConnectionString").ToString)
Dim str As String
Dim sel As SqlCommand
Dim myReader As SqlDataReader
con.Open()
str = "Select id, title, keywords, speaker, date_text, duration
from webcasts where id=" & Val(Request.QueryString("id")) and speaker=" &
"'" & textbox_speaker_requested.text &"'"
sel = New SqlCommand(str, con) : myReader = sel.ExecuteReader()
If myReader.Read() Then
If (myReader.IsDBNull(0)) = False Then Label_id.Text =
myReader.GetInt32(0)
If (myReader.IsDBNull(1)) = False Then TextBox_title.Text =
myReader.GetString(1).Trim
If (myReader.IsDBNull(2)) = False Then TextBox_keywords.Text
= myReader.GetString(2).Trim
If (myReader.IsDBNull(3)) = False Then TextBox_speakers.Text
= myReader.GetString(3).Trim
If (myReader.IsDBNull(4)) = False Then
TextBox_daterecorded.Text = myReader.GetString(4).Trim
If (myReader.IsDBNull(5)) = False Then TextBox_duration.Text
= myReader.GetInt32(5)
End If
myReader.Close() : con.Close()
netnatter

Nov 4 '08 #1
2 2026
In most cases you should not use a DataReader. The reason is that until you
call close you are keeping a connection open. It's much better to use a
DataSet since the time connected to the database is usually much shorter. I
tend to use typed DataSets since they are much easier to use or you can use
the new Entity Framework.

If you still want to use DataReader, PLEASE use Exception handeling and
close the reader and connection in the Finally block.

To actually answer your question you can put parameters in your SQL string
and then use parameter objects to set them. There are tons of examples if you
Google it.

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://www.cafepress.com/geekmusicart.1654787045
"netnatter" wrote:
>

I have used the following to get data from my SQL database and populate text
& label boxes.

I understand how to use parameters with gridview and data view but not with
the reader.

How can I change this code to use parameters and so give some protection
from insertion issues.

Dim con As New
SqlConnection(ConfigurationManager.ConnectionStrin gs("mydataConnectionString").ToString)
Dim str As String
Dim sel As SqlCommand
Dim myReader As SqlDataReader
con.Open()
str = "Select id, title, keywords, speaker, date_text, duration
from webcasts where id=" & Val(Request.QueryString("id")) and speaker=" &
"'" & textbox_speaker_requested.text &"'"
sel = New SqlCommand(str, con) : myReader = sel.ExecuteReader()
If myReader.Read() Then
If (myReader.IsDBNull(0)) = False Then Label_id.Text =
myReader.GetInt32(0)
If (myReader.IsDBNull(1)) = False Then TextBox_title.Text =
myReader.GetString(1).Trim
If (myReader.IsDBNull(2)) = False Then TextBox_keywords.Text
= myReader.GetString(2).Trim
If (myReader.IsDBNull(3)) = False Then TextBox_speakers.Text
= myReader.GetString(3).Trim
If (myReader.IsDBNull(4)) = False Then
TextBox_daterecorded.Text = myReader.GetString(4).Trim
If (myReader.IsDBNull(5)) = False Then TextBox_duration.Text
= myReader.GetInt32(5)
End If
myReader.Close() : con.Close()
netnatter

Nov 4 '08 #2
"dotNetDave" <do********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.com...
If you still want to use DataReader, PLEASE use Exception handling and
close the reader and connection in the Finally block.
Or, even better, don't... Instead, use a using {} block:
http://www.aspfree.com/c/a/ASP.NET/U...eader-Class/2/
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 4 '08 #3

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

Similar topics

2
by: Michael | last post by:
A webpage has the following js: function openPDF(PDFdoc) { // Open a blank window and target PDFdoc into it. // PDFdoc = web address (URL) of the PDF document to present in a pop-up window
4
by: Greg Miller | last post by:
Currently I am launching adobe reader using the following call: os.system("path.file.pdf") this works fine for opening the pdf doc at the beginning. We would like to enhance this and open the...
1
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
0
by: CW | last post by:
I get this FieldCount error when I attempt to bind a datagrid with a dataset, not a datareader object. The code snippet is as belows: 'PopulateForm called in Page Load event Private Sub...
6
by: Jim Heavey | last post by:
I am new to SqlServer, have been using Oracle in a prior life. I have written a very simple stored procedure in SQL Server and it (the query) looks like the following... Select AD_ID,...
1
by: Jim Heavey | last post by:
I have a SQL stored procedures which evaluate the values passed in parameters and then based on those values reads the appropriate records. I run the stored procedure in VS 2005 Debug mode and it...
1
by: weird0 | last post by:
The property of GridView1.HasRows is true and after assigning GridView.DataSource=reader, it does not display any records in the GirdView. I am i missing out something? Here is the code:...
2
by: weird0 | last post by:
Below is the code I wrote but it aint working.... : It throws an exception on DataBind() statement that "the source does not support server-side paging". PLz Help. Really stuck. protected...
0
by: bill | last post by:
Can someone please show me what to do with this to show the data in a dataGrid for example? I'm not really sure what to do with it. I'm used to Vb6. Dim Con = New...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.