Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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

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
HarmnKardn
Asked:
HarmnKardn
  • 3
  • 2
1 Solution
 
AndrewKCommented:
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
 
HarmnKardnAuthor Commented:
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
 
AndrewKCommented:
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
 
HarmnKardnAuthor Commented:
Dude you 0wN, that was exactly the problem.  I am increasing the points and accepting your answer.  Much thanks!
0
 
AndrewKCommented:
Much obliged!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now