473,387 Members | 1,585 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,387 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 5353
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.