473,325 Members | 2,608 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,325 software developers and data experts.

C# Code Problem - Help

76
Please, can anyone tell me what's wrong with the following C# code.

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2.     {
  3.         SqlConnection       ConnectionSql = null;
  4.         SqlCommand          CommandSql = null;
  5.         SqlDataReader         QueryResult = null;
  6.  
  7.         public MyClass(string dbString)
  8.         {
  9.             ConnectionSql = new SqlConnection(dbString);
  10.             CommandSql = ConnectionSql.CreateCommand();
  11.             CommandSql.Connection = ConnectionSql;
  12.         }
  13.  
  14.         public string ReturnSomething(string sql, int colIndex)
  15.         {
  16.             ConnectionSql.Open();
  17.             CommandSql.CommandText = sql;
  18.             CommandSql.Connection = ConnectionSql;
  19.             QueryResult = CommandSql.ExecuteReader();
  20.             return (QueryResult[colIndex].ToString());
  21.         }
  22.     }    
  23.  
  24.  
Aug 5 '06 #1
2 1447
Began
3
hi,

change the ReturnSomething method as below.

public string ReturnSomething(string sql, int colIndex)
{
ConnectionSql.Open();
CommandSql.CommandText = sql;
CommandSql.Connection = ConnectionSql;
QueryResult = CommandSql.ExecuteReader();
if (QueryResult.Read())
{
return (QueryResult[0].ToString());
}
else
{
return null;
}
}


regards,
Began.
Aug 7 '06 #2
YL99
1
Please, can anyone tell me what's wrong with the following C# code.

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2.     {
  3.         SqlConnection       ConnectionSql = null;
  4.         SqlCommand          CommandSql = null;
  5.         SqlDataReader         QueryResult = null;
  6.  
  7.         public MyClass(string dbString)
  8.         {
  9.             ConnectionSql = new SqlConnection(dbString);
  10.                         ConnectionSql.Open();
  11.             CommandSql = ConnectionSql.CreateCommand();
  12.             CommandSql.Connection = ConnectionSql;
  13.         }
  14.  
  15.         public string ReturnSomething(string sql, int colIndex)
  16.         {
  17.             CommandSql.CommandText = sql;
  18.             QueryResult = CommandSql.ExecuteReader();
  19.  
  20.                        if(QueryResult.read) // To ensure that we have something to read 
  21.                          // else it will throw an exception
  22.                           {                              
  23.                      return (QueryResult[colIndex].ToString());
  24.                           }
  25.         }
  26.     }    
  27.  
  28.  
Debug it, so you will be able to ascertain whether the connection is null or open
Aug 7 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.