Reading from a database using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #1

    Reading from a database using C#

    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):

    Code:
     int NoOfProducts = ds.Tables["Connectors"].Rows.Count;
    
            int i = 0;
            if (NoOfProducts == 0)
            {
                lbNoOfProducts.Text = "<p>Your search produced no results</p>";
                lbNoOfProducts.Visible = true;
            }
    		
    		else if(NoOfProducts == 1)
    		{
    			Response.Redirect("http://www.website.com/search.aspx?pn="+Eval("PartNumber")+"");
    		}
    		
            else
            {
                lbNoOfProducts.Text = "<p>Displaying " + NoOfProducts + " products</p>";
                lbNoOfProducts.Visible = true;
                if (ds.Tables["Connectors"].Rows[0]["PartNumber"] != "")
                {
                    string strPartNumber = (Convert.ToString(ds.Tables["Connectors"].Rows[0]["PartNumber"]));
    
                }
            }
    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
  • DaveRook
    New Member
    • Jul 2007
    • 147

    #2
    Solved

    I worked it out!

    Code:
    string PN = (Convert.ToString(ds.Tables["Connectors"].Rows[0]["PartNumber"]));
    Response.Redirect("http://www.website.com/connect.aspx?pn="+PN+"");
    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

    Comment

    Working...