472,958 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 5348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
2
by: Enrico Pangan | last post by:
I'm trying to call some functions in a C++ Dll, "Library.dll" from C#. Some functions work but some return the NullReferenceException. I have here the source code for the C++ version and for the...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.