Click here to Skip to main content
15,895,772 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys me again.

got a little problem with the code finding the table with the index of 0 (zero)

i dont see the problem my self as i am using only one table.

this is the code for my update function:

C#
private void UpdateBTN_Click(object sender, EventArgs e)
        {
            using (conn)
            {
                da.UpdateCommand = new SqlCommand("UPDATE Book SET Title = @title, AuthorFirstName = @authorFirstName, AuthorSurname = @authorsurname, AquisitionDate = @aquisitiondate, PublisherName = @publishername,
 PublisherCity = @publishercity, PublishingDate = @publishingdate,
 ISBN = @isbn, DDClassification = @ddclassification WHERE BookID = @ID", conn);

da.UpdateCommand.Parameters.Add("@title", SqlDbType.VarChar).Value = titleTB.Text;
da.UpdateCommand.Parameters.Add("@authorFirstName", SqlDbType.VarChar).Value = authorFirstNameTB.Text;
da.UpdateCommand.Parameters.Add("@authorsurname", SqlDbType.VarChar).Value = authorSurNameTB.Text;
da.UpdateCommand.Parameters.Add("@aquisitiondate", SqlDbType.Date).Value = aquisitionDateTB.Text.ToString();
da.UpdateCommand.Parameters.Add("@publishername", SqlDbType.VarChar).Value = publisherNameTB.Text;
da.UpdateCommand.Parameters.Add("@publishercity", SqlDbType.VarChar).Value = publisherCityTB.Text;
da.UpdateCommand.Parameters.Add("@publishingdate", SqlDbType.Date).Value = publishingDateTB.Text.ToString();
da.UpdateCommand.Parameters.Add("@isbn", SqlDbType.VarChar).Value = ISBNTB.Text;
da.UpdateCommand.Parameters.Add("@ddclassification", SqlDbType.VarChar).Value = DDClassificationTB.Text;
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];// right here @ ds.Tables[0]
conn.Open();
da.UpdateCommand.ExecuteNonQuery();
       }
        }



error is on last update parameter, any idea why it is throwing this exception?

message is:
IndexOutOfRangeException was unhandled

Cannot find table 0.

Regards
Matt.
Posted

ds contains no tables at that point in the code, that seems to be your problem.
 
Share this answer
 
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];// right here @ ds.Tables[0]
There is error at ds.Tables[0]

Use Visual Studio DEBUGGER and check what does the dataset contains. If there is any table in it or not. Clearly there is no table in it.
I don't see a ds defined in the above method. So, make sure whenever you are filling your dataset, it persists and make sure that before using it's value you check for table count.
 
Share this answer
 
v2
Comments
BBCokeley 2-May-12 13:24pm    
now getting connection string has not been initialised, although it is stated at the top, and im using the Using (conn) clause, with conn.open() before executing the query
BBCokeley 2-May-12 13:37pm    
is it not retriving the conection string?

SqlConnection conn = new SqlConnection("Data Source=MATTHEW-PC\\SQLEXPRESS;Initial Catalog=BookDetails2;Integrated Security=True");
Sandeep Mewara 3-May-12 1:10am    
What is ds.Tables[0] ? And where from you get that?
fixed this myself, i was passing an empty dataset :(,
noticed it earlier. thanks for reply's much appreciated
 
Share this answer
 
Comments
wizardzz 8-May-12 11:36am    
Hi BB, you're welcome. Glad you are not stuck. This isn't really a solution though.
BBCokeley 8-May-12 11:49am    
managed to figure it out and pass full tables into the dataset, sorry should have been more clear.
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900