?
Solved

What could be used in C# instead of Reader?

Posted on 2015-01-23
3
Medium Priority
?
83 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 2000 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

Technology Partners: 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 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