Click here to Skip to main content
15,900,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello again, I have a problem here, whenever i click the Update button on my program, this error always pop out "There is no row at position 2.", i really need your help my deadline is tomorrow, thanks!

VB
Dim cb2 As New OleDbCommandBuilder(da2)
da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
da2.Fill(ds2, "PurchaseDB2")
        For i = 0 To poDG.Rows.Count - 1
            ds2.Tables("PurchaseDB2").Rows(RowNumber).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(RowNumber).Item("Description") = poDG.Rows.Item(i).Cells(1).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(RowNumber).Item("Qty") = poDG.Rows.Item(i).Cells(2).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(RowNumber).Item("UnitPrice") = poDG.Rows.Item(i).Cells(3).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(RowNumber).Item("Amount") = poDG.Rows.Item(i).Cells(4).Value.ToString
            da2.Update(ds2, "PurchaseDB2")
        Next
Posted

ok..
i have sample code whcih updates the grid rows into the DB table but it uses a Stored procedure.
Here it is : maybe u can modify it to suit ur appl.
This is the code in teh save button click event handler:

DataSet objDataSet = new DataSet();
objDataAdapter.Fill(objDataSet);
grdExcel.DataSource = objDataSet.Tables[0].DefaultView;
grdExcel.DataBind();

////
SqlConnection conn = new SqlConnection(connstring);

conn.Open();
SqlCommand comm = new SqlCommand("sp_insert", conn);
comm.CommandType = CommandType.StoredProcedure;

int i;
for (i = 0; i <= grdExcel.Rows.Count - 1; i++)
{
comm.Parameters.Add("@lastname", SqlDbType.VarChar).Value =objDataSet .Tables [0].Rows[i][0].ToString ();
comm.Parameters.Add("@firstname", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][1].ToString();
comm.Parameters.Add("@address1", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][2].ToString();
comm.Parameters.Add("@address2", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][3].ToString();
comm.Parameters.Add("@city", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][4].ToString();
comm.Parameters.Add("@state", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][5].ToString();
comm.Parameters.Add("@zip", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][6].ToString();
comm.Parameters.Add("@phone", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][7].ToString();
comm.Parameters.Add("@fax", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][8].ToString();
comm.Parameters.Add("@email", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][9].ToString();
comm.Parameters.Add("@website", SqlDbType.VarChar).Value = objDataSet.Tables[0].Rows[i][10].ToString();

int rows;
rows = comm.ExecuteNonQuery();
comm.Parameters.Clear();
}

Sp_Insert is teh stored procedure im using.

create proc [dbo].[sp_insert](
@lastname varchar(50),
@firstname varchar(50),
@address1 varchar(50),
@address2 varchar(50),
@city varchar(50),
@state varchar(50),
@zip varchar(50),
@phone varchar(50),
@fax varchar(50),
@email varchar(50),
@website varchar(50)) as

INSERT INTO [Ann].[dbo].[Members]
([lastname]
,[firstname]
,[address1]
,[address2]
,[city]
,[state]
,[zip]
,[phone]
,[fax]
,[email]
,[website]
)
VALUES
(@lastname
,@firstname
,@address1
,@address2
,@city
,@state
,@zip
,@phone
,@fax
,@email
,@website
);

select scope_identity()
 
Share this answer
 
i managed to fix the errors now it's working with no errors, but it's still not working although the program is telling me that the database has been updated but when i check the database, inputs in datagridview didn't save while the inputs for textboxes are there in the database. Can you give me a sample code on how to fix this? tyvm.
 
Share this answer
 
dear..
please put abreakpoint in that function and debug it..
when it hits the for loop check what the value for count is..
have a look at the poDG before you step into the for loop itself
 
Share this answer
 

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