By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,190 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,190 IT Pros & Developers. It's quick & easy.

Reading from a database using C#

100+
P: 147
Hello

I have a website which uses an Access databse.

The user makes a selection and the answer/s are shown.

I have some code which counts the amount of records and then either displays an error message or shows the products.

What I would like to do is work out if there is only 1 record and what the name of the 1 serial number is for that record.

EG, my database has 2 columns
ID and SerialNumber

Simply put, my ideal code would be

If there is only 1 record, then get the SerialNumber. Then, Redirect to www. My Serial Number .com

My code so far (Please note it is Line 12 which has the problem):

Expand|Select|Wrap|Line Numbers
  1.  int NoOfProducts = ds.Tables["Connectors"].Rows.Count;
  2.  
  3.         int i = 0;
  4.         if (NoOfProducts == 0)
  5.         {
  6.             lbNoOfProducts.Text = "<p>Your search produced no results</p>";
  7.             lbNoOfProducts.Visible = true;
  8.         }
  9.  
  10.         else if(NoOfProducts == 1)
  11.         {
  12.             Response.Redirect("http://www.website.com/search.aspx?pn="+Eval("PartNumber")+"");
  13.         }
  14.  
  15.         else
  16.         {
  17.             lbNoOfProducts.Text = "<p>Displaying " + NoOfProducts + " products</p>";
  18.             lbNoOfProducts.Visible = true;
  19.             if (ds.Tables["Connectors"].Rows[0]["PartNumber"] != "")
  20.             {
  21.                 string strPartNumber = (Convert.ToString(ds.Tables["Connectors"].Rows[0]["PartNumber"]));
  22.  
  23.             }
  24.         }
I know this won't work as it's not part of a databound control and so I can't use the Eval.

How do I just read this value from the databse and insert it into (line12)?

Thanks

Dave
Feb 12 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 147
I worked it out!

Expand|Select|Wrap|Line Numbers
  1.  
  2. string PN = (Convert.ToString(ds.Tables["Connectors"].Rows[0]["PartNumber"]));
  3. Response.Redirect("http://www.website.com/connect.aspx?pn="+PN+"");
  4.  
  5.  
However, I still have a question. Where it shows Rows[0], why is it 0? Is it 0 because 0 is 1? If I had done Rows[10] would it have showed the PartNumber for Row11

Dave
Feb 12 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.