473,513 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong with this code? Trying to grab a DataReader

hello,

I am writing my first real ASP.NET application, but am getting a bit
confused with ADO.NEt and the best way to use it.

My old method (with Classic ASP) was to have a function that you called
like ...

Set rsSomething = GrabRS("select * from mytable")

The function would open a connection to the database, pull out the data
and return a disconnected recordset. This could then be used in the ASP.

I want to do something similar in ADO.NET and ASP.NET, but am unsure of
the best way to do it. I have got as far as ...

SqlDataReader GrabDS(String strSQL) {
SqlConnection conConn = New
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
conConn.Open();
SqlCommand comComm = new SqlCommand(strSQL, conConn);
SqlDataReader drdData = comComm.ExecuteReader();
return drdData;
}

but keep getting an error on the first line of the block telling me
there is a missing colon. I have tried all sorts of variations of this,
but can't work out what's wrong.

Any advice? TIA for any help.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
9 1319
Before anyone wastes their time, and to help any future readers, i got
this working just after I posted (ain't that always the way?). The
following code did what I wanted ...

SqlDataReader GrabDR(String strSQL) {
SqlConnection conConnection;
SqlCommand cmdCommand;
SqlDataReader dtrData;

conConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
conConnection.Open();
cmdCommand = new SqlCommand(strSQL, conConnection);
dtrData = cmdCommand.ExecuteReader();
return dtrData;
}
I'm not saying this is the most efficient way to do it, and if anyone
has any suggestions for improvement, I would be very grateful to hear
them, but this does work.
hello,

I am writing my first real ASP.NET application, but am getting a bit
confused with ADO.NEt and the best way to use it.

My old method (with Classic ASP) was to have a function that you called
like ...

Set rsSomething = GrabRS("select * from mytable")

The function would open a connection to the database, pull out the data
and return a disconnected recordset. This could then be used in the ASP.

I want to do something similar in ADO.NET and ASP.NET, but am unsure of
the best way to do it. I have got as far as ...

SqlDataReader GrabDS(String strSQL) {
SqlConnection conConn = New
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
conConn.Open();
SqlCommand comComm = new SqlCommand(strSQL, conConn);
SqlDataReader drdData = comComm.ExecuteReader();
return drdData;
}

but keep getting an error on the first line of the block telling me
there is a missing colon. I have tried all sorts of variations of this,
but can't work out what's wrong.

Any advice? TIA for any help.


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #2
On 15 Feb 2005, Alan Silver <al*********@nospam.thanx> postulated in
news:xv**************@nospamthankyou.spam:
Before anyone wastes their time, and to help any future readers, i got this working just after I posted (ain't that always the way?). The
following code did what I wanted ...

SqlDataReader GrabDR(String strSQL) {
SqlConnection conConnection;
SqlCommand cmdCommand;
SqlDataReader dtrData;

conConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
conConnection.Open();
cmdCommand = new SqlCommand(strSQL, conConnection);
dtrData = cmdCommand.ExecuteReader();
return dtrData;
}
I'm not saying this is the most efficient way to do it, and if anyone has any suggestions for improvement, I would be very grateful to hear them, but this does work.
hello,

I am writing my first real ASP.NET application, but am getting a bitconfused with ADO.NEt and the best way to use it.

My old method (with Classic ASP) was to have a function that you calledlike ...

Set rsSomething = GrabRS("select * from mytable")

The function would open a connection to the database, pull out the dataand return a disconnected recordset. This could then be used in the ASP.
I want to do something similar in ADO.NET and ASP.NET, but am unsure ofthe best way to do it. I have got as far as ...

SqlDataReader GrabDS(String strSQL) {
SqlConnection conConn = New
SqlConnection(ConfigurationSettings.AppSetting s["ConnStr"]);
conConn.Open();
SqlCommand comComm = new SqlCommand(strSQL, conConn);
SqlDataReader drdData = comComm.ExecuteReader();
return drdData;
}

but keep getting an error on the first line of the block telling me
there is a missing colon. I have tried all sorts of variations of this,but can't work out what's wrong.

Any advice? TIA for any help.


There's nothing wrong, per se, but you would be safer to use

1. stored procedures if possible, and if not, at least a
parameterized query

2. a try / catch block around your call to the server. This way you
can handle your error for the user and it also gives you an
opportunity to return a (SqlDataReader) null to the caller if no data
is returned.
-- ipgrunt

Nov 19 '05 #3
>There's nothing wrong, per se, but you would be safer to use

1. stored procedures if possible, and if not, at least a parameterized
query
Good point, however this code was supposed to be generic and so would be
harder with a sp. I could of course pass a call to an sp in the
parameter, it doesn't have to be plain SQL.
2. a try / catch block around your call to the server. This way you can
handle your error for the user and it also gives you an opportunity to
return a (SqlDataReader) null to the caller if no data is returned.


Ah, very good point. This was as far as I got when I got it working, so
I posted it before adding any checking. It did occur to me to add some,
but I must confess I haven't added it yet.

Thanks very much for the comments.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #4
On 15 Feb 2005, Alan Silver <al*********@nospam.thanx> postulated in
news:hK**************@nospamthankyou.spam:
There's nothing wrong, per se, but you would be safer to use

1. stored procedures if possible, and if not, at least a parameterizedquery
Good point, however this code was supposed to be generic and so

would be harder with a sp. I could of course pass a call to an sp in the
parameter, it doesn't have to be plain SQL.
2. a try / catch block around your call to the server. This way you canhandle your error for the user and it also gives you an opportunity toreturn a (SqlDataReader) null to the caller if no data is returned.
Ah, very good point. This was as far as I got when I got it

working, so I posted it before adding any checking. It did occur to me to add some, but I must confess I haven't added it yet.

Thanks very much for the comments.


A Pleasure.

Sounds like you have it under control.

You might consider implementing your generic routine as a data access
class, where you can pass variable number and types of parameters in
as a constructor parameter.

Have fun.

-- ipgrunt

Nov 19 '05 #5
>A Pleasure.

;-)
Sounds like you have it under control.
That might be going too far, but I do have some idea of what I'm doing
now!!
You might consider implementing your generic routine as a data access
class, where you can pass variable number and types of parameters in
as a constructor parameter.
Which leads me to the next question I was going to ask ...

Once I have a useful routine like this (and its variant friends who will
soon appear), how do I go about making them globally available to any
web site on the machine without having to add them to each site? Common
routines like this should (presumably) be housed in one central location
where they can be accessed by any page on any site on the machine.

I'm quite new at this, so please explain clearly otherwise you'll lose
me!!
Have fun.


I am actually. I am a 100% convert from Classic ASP. I'm being more and
more amazed every time I find something else you can do in ASP.NET. I
wrote a script yesterday that would have taken me far longer in Classic
ASP, even with the library routines I have built up. I'm sure I could
have done it even faster had it not been the first time I had done a lot
of the things I was doing. I guess it will be a lot easier next time.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6
On 16 Feb 2005, Alan Silver <al*********@nospam.thanx> postulated in
news:8I**************@nospamthankyou.spam:
A Pleasure.
;-)
Sounds like you have it under control.


That might be going too far, but I do have some idea of what I'm

doing now!!
You might consider implementing your generic routine as a data accessclass, where you can pass variable number and types of parameters inas a constructor parameter.
Which leads me to the next question I was going to ask ...

Once I have a useful routine like this (and its variant friends who

will soon appear), how do I go about making them globally available to any web site on the machine without having to add them to each site? Common routines like this should (presumably) be housed in one central location where they can be accessed by any page on any site on the machine.

I'm quite new at this, so please explain clearly otherwise you'll lose me!!
Have fun.
I am actually. I am a 100% convert from Classic ASP. I'm being more

and more amazed every time I find something else you can do in ASP.NET. I wrote a script yesterday that would have taken me far longer in Classic ASP, even with the library routines I have built up. I'm sure I could have done it even faster had it not been the first time I had done a lot of the things I was doing. I guess it will be a lot easier next time.
Thanks for the reply.


OK.... you need to learn about static classes.
I declare the class like this:

public class Util
{

...

and then your methods get declared as static...
public static HttpContext getUserContext ()
{
return System.Web.HttpContext.Current;
}

Now this method is available to all in the namespace as:

HttpContext curr = Util.getUserContext();
Very simple, right?
By the way, I too was an ASPer and was resistent to learning *yet
another* set of programming conventions. But I'm glad I did. I abhor
writing for ASP anymore.

What I did was take the survey course that covered VisualStudio.net,
ADO.NET, ASP.NET, the Framework, etc., which really kickstarted my
learning. (I recommend that you try to get your employer/clients to
pay for it!)
-- ipgrunt
Nov 19 '05 #7
<snip>
Very simple, right?
Yup, once you know it!!

As soon as I saw it, I realised what I was doing wrong. I did some Java
a few years ago, and it was enough to kick my brain into gear when I saw
the answer posted. It's just a case of getting your head into a new
frame of thinking.
By the way, I too was an ASPer and was resistent to learning *yet
another* set of programming conventions. But I'm glad I did. I abhor
writing for ASP anymore.
Well, I've only been doing this for a few weeks, and it's only the last
couple of days that I've written anything that could be called Real
World(tm) stuff, so I'm still quite slow at it, but I can see already
that it knocks the spots of Classic ASP. This stuff is brilliant,
there's just so much here that can save wads of time.
What I did was take the survey course that covered VisualStudio.net,
ADO.NET, ASP.NET, the Framework, etc., which really kickstarted my
learning. (I recommend that you try to get your employer/clients to pay
for it!)


I *am* my employer <g>

Where do I find out about this course? I don't know if I can afford it,
but it's worth looking.

I've been working my way through ASP.NET Unleashed, which is very good.
Between that and some great help in this group, I'm getting the hang of
it all.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #8
On 16 Feb 2005, Alan Silver <al*********@nospam.thanx> postulated in
news:+b**************@nospamthankyou.spam:
<snip>
Very simple, right?
Yup, once you know it!!

As soon as I saw it, I realised what I was doing wrong. I did some

Java a few years ago, and it was enough to kick my brain into gear when I saw the answer posted. It's just a case of getting your head into a new
frame of thinking.
By the way, I too was an ASPer and was resistent to learning *yet
another* set of programming conventions. But I'm glad I did. I abhorwriting for ASP anymore.
Well, I've only been doing this for a few weeks, and it's only the

last couple of days that I've written anything that could be called Real
World(tm) stuff, so I'm still quite slow at it, but I can see already that it knocks the spots of Classic ASP. This stuff is brilliant,
there's just so much here that can save wads of time.
What I did was take the survey course that covered VisualStudio.net,ADO.NET, ASP.NET, the Framework, etc., which really kickstarted my
learning. (I recommend that you try to get your employer/clients to payfor it!)
I *am* my employer <g>

Where do I find out about this course? I don't know if I can afford

it, but it's worth looking.

I've been working my way through ASP.NET Unleashed, which is very good. Between that and some great help in this group, I'm getting the hang of it all.

Thanks for the reply.


I am my employer (unemployer?), too. That's why I suggested getting a
client to pay.

These are Microsoft courses and taught by various shops around the
country. The one I took was in Bethesda, MD and given by a group from
VA somewhere, but that's cause my client was there and I got to sit
in with a group of their developers.

First we took a two-day course:

http://www.microsoft.com/learning/sy...717Bfinal.mspx

This was the key to getting us kickstarted and if you can, get to
this course.

The next week we built our own learning (this teacher was good and
taught it all) out of bits and pieces of what seemed like these two
courses, (can't find the specifics as this was two or three years
ago.)
http://www.microsoft.com/learning/sy...349bfinal.mspx

http://www.microsoft.com/learning/sy...310Bfinal.mspx

The course did not have the detail that these seem to offer in some
areas, and provided much more detail in others... (which was
convenient, as these courses go to slowly for two days while students
get in gear and too fast for two days where it really counts as the
instructor is trying to catch up.)

We were focused on porting a large webapp from Java to .NET and
needed specifics about ADO.NET with Oracle, assemblies vs. libraries,
scriptlet issues, use of delagates for call-backs, and a dozen other
issues. I was hired because the client's programmers had zero
Microsft background and I was a bigtime ASP developer (so I told
them). Turns out, my knowledge was almost useless...the things I
learned from my experience with ASP didn't apply anymore. (like don't
put COM objects in Session vars, consolidate globals and utility
functions in include files (sound familiar), etc.)

A word of advice, use your Java skills, NOT your ASP skills when
architecting your application.

There are probably online learning course you could take that would
a) save dollars and b) save time. You need to be disciplined to do
this, and maybe your book gives you all you need?

But, there is something about an instructor led course, the
interaction with other students, the nights back at the hotel bar
telling lies, that really gets things moving in the brain to learn a
new technology. Of course, you have to keep applying your knowledge
once you get back home or you lose it quickly.

One more tip... get to know the Patterns and Practices site. This is
the best thing since sliced bread (and class factories) on msdn for
experienced developers.

http://www.microsoft.com/resources/p...s/default.mspx

Now, I gotta get to work! If you need to you can email me at the name
below via yahoo mail.

-- ipgrunt
Nov 19 '05 #9
<snip>

Thanks for the comments. Some back for you ...
A word of advice, use your Java skills, NOT your ASP skills when
architecting your application.
Spotted that one!! Trouble is, it's a few years since I did any Java, so
I'm basically remembering not to do it the ASP way!! Thinking in Java is
as hard as thinking in C# at the moment, so I'm just concentrating on
the latter.
There are probably online learning course you could take that would
a) save dollars and b) save time. You need to be disciplined to do
this, and maybe your book gives you all you need?
So far the book and this newsgroup have been excellent. The book gives
me the ideas and basic code, the group gives me feedback and ideas on
how to do more complex stuff.
One more tip... get to know the Patterns and Practices site. This is
the best thing since sliced bread (and class factories) on msdn for
experienced developers.

http://www.microsoft.com/resources/p...s/default.mspx


I had a look at this, but at the time it was beyond me. I may go back
and look again though now I have more idea what I'm doing.

Thanks again,

Alan

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #10

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

Similar topics

10
2041
by: G Matthew J | last post by:
interesting "signal vs. noise" blog entry: http://37signals.com/svn/archives2/whats_wrong_with_ajax.php
121
9909
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
13
5002
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
5
5748
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
3
1312
by: shin | last post by:
hi i am trying to output an image which i have in a database. but this gives me errors byte Picture; Picture = (dr); //cannot convert from object to byte Response.Buffer=true;
5
1194
by: Tim::.. | last post by:
Can someone please tell me what is wrong with the following code! I have a record in the datareader but it isn't showing up and niether is the label... I presume there is a problem with this...
10
2237
by: Daniel Sélen Secches | last post by:
I was trying to make a simple dataReader.... Sub projets() Dim conn As New OleDbConnection(connstring) Dim dr As OleDbDataReader() Dim cmd As New OleDbCommand() cmd.CommandText = "select *...
2
1009
by: Frank | last post by:
Hi All, I have a task at hand and I am trying to plan out how I should approach it. I can use either VS.NET 2003 or 2005 I need to grab data out of an Access 97 db and push it to SQL Server...
4
6182
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Here is what I have: private int NationalCount() { Int32 numRecords = 0; using (SqlConnection dataConnection = new SqlConnection(GlobalVars.sqlConnString)) { SqlCommand dataCommand = new...
0
7260
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
7384
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
7539
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
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
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
3234
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
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1597
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 ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.