Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
cn.Open()
cmd.CommandText = "INSERT INTO CoursesLines (FieldID, CoursID, CoursDescr) VALUES (@FieldID2, @CourseID, @Descr)"
cmd.Parameters.AddWithValue("@FieldID2", Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value)
cmd.Parameters.AddWithValue("@CourseID", Me.DataGridView1.Item(1, Me.DataGridView1.CurrentRow.Index).Value)
cmd.Parameters.AddWithValue("@Descr", Me.DataGridView1.Item(2, Me.DataGridView1.CurrentRow.Index).Value)
cmd.ExecuteNonQuery()
cn.Close()
Posted
Comments
David Lee 145 29-Aug-14 17:33pm    
Because you've updated CurrentRow.

See below page. It may help you.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/fdb10de8-1cf5-4692-b7b1-4fcd38875e24/datagridviewcurrentrow-vs-selectedrow
Aiman Ali 30-Aug-14 11:13am    
the link didnt really help. my issue is i want a loop in my insert statement to insert each line to my table.

 
Share this answer
 
Comments
Aiman Ali 2-Sep-14 2:09am    
I tried using datasource to connect to my datagrid but it gives me login failed. Plz provide sample code i can test with. thnks
MuhammadUSman1 2-Sep-14 2:50am    
Please paste your code, and identify where you are facing this issue(exception).
Aiman Ali 2-Sep-14 4:00am    
i have 3 columns in my datagridview. with the code i jst shared it only insert the index value 0,1,2 lol how to get the actual string.
Aiman Ali 2-Sep-14 3:57am    
cmd.CommandText = "INSERT INTO CoursesLines (FId, CoursID, CoursDescr) VALUES (@FId, @CoursID, @CoursDescr)"
cmd.Parameters.AddWithValue("@FId", Fld.DisplayIndex.ToString)
cmd.Parameters.AddWithValue("@CoursID", CoursID.DisplayIndex.ToString)
cmd.Parameters.AddWithValue("@CoursDescr", CoursDescr.DisplayIndex.ToString)

'cmd.Connection = cn
Try
For i As Integer = 0 To DataGridView1.Rows.Count - 1
cmd.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
cmd.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).Value
cmd.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).Value

cmd.ExecuteNonQuery()
Next
cn.Close()
Catch ex As Exception
MsgBox("" + ex.Message)
dr.Close()
End Try
MuhammadUSman1 2-Sep-14 6:26am    
Check my solution. and do according to your requirements.
if any question then let me know.
thanx

C#
// this is helping code to please check.

class CodeProject_Solution
    {
        SqlConnection con = new SqlConnection("your constring");
        SqlCommand cmd = new SqlCommand("select * form CoursesLines");
        DataTable dtTemp = new DataTable();

        private void UpdateSolution()
        {
            //open connection
            con.Open();
            cmd.Connection = con;

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            builder.ConflictOption = ConflictOption.OverwriteChanges;//to insert update and delete data and also update dtTemp            

            adapter.Fill(dtTemp);
        }

        private void AddMultiValues()
        {
            for (int i = 0; i < 50; i++)
            {
                DataRow dr = dtTemp.NewRow();
                dr["FId"] = "id" + i;
                dr["CoursID"] = "CoursID" + i;
                dr["CoursDescr"] = "CoursDescr" + i;
                dtTemp.Rows.Add(dr);
            }
            dtTemp.AcceptChanges();
            //know call function
            UpdateSolution();
            //know all 50 rows will be inserted in db. and also update DataTable and CoursesLines(in database)
        }
    }


if any query please let me know.

Thanks.

helping link.
Updating Data Sources with DataAdapters
 
Share this answer
 
v2

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