?
Solved

SQL - DataSet not filling / cannot see a table in the DB

Posted on 2003-02-24
5
Medium Priority
?
204 Views
Last Modified: 2010-04-15
I've done this with two other tables in my project, using similar syntax on the database, works fine.  But here, my dataset doesn't seem to be filling properly.  My ds.Tables["UserButtons"] is always null in the QuickWatch.  I've tried changing table names, recreating the table, restarting the DB, none of which have gotten me anywhere on this problem.

I'm hoping somebody will see a stupid mistake that I couldn't find in two days.  Code is below.  Much thanks.


//locals
private string sUser;

//handles our database connection and data
private SqlConnection     sqlConn;
private DataSet ds = new DataSet();
private SqlDataAdapter da = new SqlDataAdapter();

public UserButtonsInterface(string sUserId)
{
     sUser = sUserId.ToUpper();

     //connect to database
     string sSql;
     string sConn = "Initial Catalog=MyDatabase;Data Source=SQLSERV2;Integrated Security=SSPI;";
     sqlConn = new SqlConnection(sConn);

     //get the dataset with the User settings
     sSql = "SELECT * FROM UserButtons WHERE UserId LIKE '"
          + sUser.ToString() + "'";
     da.SelectCommand = new SqlCommand (sSql,sqlConn);
     da.Fill(ds);

     //FAILS HERE WITH "OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT"
     int iNumRecords = ds.Tables["UserButtons"].Rows.Count;
     System.Windows.Forms.MessageBox.Show("Select query found " + iNumRecords.ToString() + " rows.");
}
0
Comment
Question by:HarmnKardn
[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
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:AndrewK
ID: 8009531
Have you tried putting a wildcard after the sUser.ToString()? As in:

sSQL = "SELECT * FROM UserButtons WHERE UserId LIKE '" + sUser.ToString() + "'%'";

Otherwise, the SELECT is trying to match an exact representation of the value that sUser.ToString() returns and you may not have one.

The % wildcard is for most RDBMS - aka SQL Server. For Access, I think it is * ... can't remember though.
0
 
LVL 1

Author Comment

by:HarmnKardn
ID: 8009637
Unfortunately, that's not it.  I am in fact looking for an exact representation (so I probably don't need the "LIKE").  But that's regardless.  Even if there are no records, it shouldn't throw an object error when I try to get the Rows.Count of the table.  Still, I can't figure out why the damn table is null.
0
 
LVL 2

Accepted Solution

by:
AndrewK earned 300 total points
ID: 8009753
Do you need this line:

da.Fill(ds);

To read:

da.Fill(ds, "UserButtons");

So you can then actually reference the table name (the table name doesn't actually have to correspond to the name in the db).
0
 
LVL 1

Author Comment

by:HarmnKardn
ID: 8010049
Dude you 0wN, that was exactly the problem.  I am increasing the points and accepting your answer.  Much thanks!
0
 
LVL 2

Expert Comment

by:AndrewK
ID: 8010209
Much obliged!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

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