Solved

What could be used in C# instead of Reader?

Posted on 2015-01-23
3
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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