471,338 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

C# .NET 2.0 NullReferenceException

C# .NET 2.0
whats wrong with my code?
I get an error on the second line saying "NullReferenceException was
unhandled by user code" - "Object reference not set to an instance of
an object."

01 string sql = "select * from myTable";
02 DataTable dtStructure = Database.ExecuteDataTable(sql);
here is my function
public DataTable ExecuteDataTable(string sql)
{
// Conn and Cmd are publics
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = sql;

SqlDataAdapter sqlAdapter = new SqlDataAdapter(Cmd);

DataTable dt = new DataTable("myTable");
sqlAdapter.Fill(dt);

//close our connection
Conn.Close();

//return our DataTable
return dt;
}

Aug 1 '05 #1
5 5292
Try this.
Before you make use of Cmd, create a new instace and try.
that is
Cmd= new Command();
public DataTable ExecuteDataTable(string sql
{
// Conn and Cmd are publics
Cmd = new SqlCommand() //
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = sql;
"va*****@gmail.com" wrote:
C# .NET 2.0
whats wrong with my code?
I get an error on the second line saying "NullReferenceException was
unhandled by user code" - "Object reference not set to an instance of
an object."

01 string sql = "select * from myTable";
02 DataTable dtStructure = Database.ExecuteDataTable(sql);
here is my function
public DataTable ExecuteDataTable(string sql)
{
// Conn and Cmd are publics
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = sql;

SqlDataAdapter sqlAdapter = new SqlDataAdapter(Cmd);

DataTable dt = new DataTable("myTable");
sqlAdapter.Fill(dt);

//close our connection
Conn.Close();

//return our DataTable
return dt;
}

Aug 1 '05 #2
Hi Joby

The problem is not the function, it is this line calling the function.
DataTable dtStructure = Database.ExecuteDataTable(sql)*;

The code dies on that line

thanks

Aug 2 '05 #3
Is Database a class of yours? If so, is it a static class? And, have you
declared an instance of the class?

<va*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
C# .NET 2.0
whats wrong with my code?
I get an error on the second line saying "NullReferenceException was
unhandled by user code" - "Object reference not set to an instance of
an object."

01 string sql = "select * from myTable";
02 DataTable dtStructure = Database.ExecuteDataTable(sql);
here is my function
public DataTable ExecuteDataTable(string sql)
{
// Conn and Cmd are publics
Cmd.CommandType = CommandType.Text;
Cmd.CommandText = sql;

SqlDataAdapter sqlAdapter = new SqlDataAdapter(Cmd);

DataTable dt = new DataTable("myTable");
sqlAdapter.Fill(dt);

//close our connection
Conn.Close();

//return our DataTable
return dt;
}

Aug 2 '05 #4
Yes, thatsit. I have to treat me Database class as a static.

Aug 2 '05 #5
Try to put a break point at that line(where you are calling the function),
and examine values of all the variables and see what is null. All so step in
to the function, or a break point at the begining of the function, and see
all the values of the varibles. I really dont see any reason why it is
throwing the exception, with only couple of lines of code it is difficult to
say what is happening.

"mikev" wrote:
Hi Joby

The problem is not the function, it is this line calling the function.
DataTable dtStructure = Database.ExecuteDataTable(sql)Â*;

The code dies on that line

thanks

Aug 2 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Terrence | last post: by
5 posts views Thread by TT (Tom Tempelaere) | last post: by
2 posts views Thread by Enrico Pangan | last post: by
1 post views Thread by msnews.microsoft.com | last post: by
9 posts views Thread by Xero | last post: by
reply views Thread by rosydwin | last post: by

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.