Solved

What could be used in C# instead of Reader?

Posted on 2015-01-23
3
80 Views
Last Modified: 2015-01-23
I am new to C# and never worked with it before, but for some reason I must chose it for my SQL 2012 SSIS script task. I need to retrieve data from SQL Server database. But using Reader: reader = cmd.ExecuteReader(); my script throws an exception when Reader gets NULL value, e.g reader.GetString(x). Is there any better way to retrieve the data? And another question I am trying to assign the data to the String variable but it produce an extension. I declared it as String sStr; and using sStr = reader.GetString(x). What do I do wrong?
0
Comment
Question by:Burzhuin
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40566596
Hi Burzhuin;

The GetString method must return a non null value so you will need to test if it has a value first like in the following code snippet.

// Initialize the variable to an empty string
string sStr = "";
// Get the Field/Column only if not null 
if ( !reader.IsDBNull(x) )
   sStr = reader.GetString(x);

Open in new window


Not sure what you mean by this, "And another question I am trying to assign the data to the String variable but it produce an extension.", can you explain in a little more detail.
0
 

Author Closing Comment

by:Burzhuin
ID: 40566723
Thank you. It works. I do appreciate it.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40566826
Not a problem Burzhuin;, glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question