I am trying to insert rows data from a WPF Datagrid to a SQL database using a for
loop. Unfortunately I receive an error and I don't know what is wrong with my code. Please can you check my code?
For Z As Integer = 0 To Dawam_Grid.Items.Count - 1
Dim rd As SqlDataReader
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = "User Id='" & sb2.UserID & "';Pwd='" & sb2.Password & "';DATA SOURCE='" & sb2.DataSource & "';Initial Catalog='" & sb2.InitialCatalog & "';MultipleActiveResultSets=true"
conn.Open()
Dim ds As DataSet = New DataSet
ds.Clear() `?
cmd.Connection = conn
' dt.Columns.Add("ShowRoom_col")
' dt.Columns.Add("Job")
Dim mycommand As New SqlCommand("insert into Scedual (emp_code,Name_col,Date_Col,Shift_Col,Mor_from,Mor_to,eve_from, " _
& "eve_to,BasicSalary_col,Hour_Value_col2,Over_Hours_col2,Trans_Value_col2, " _
& "Total_OverTime_col2,Month_No,create_user,create_date,create_time,Ikama_No,Official_Hours,Day_Shift_Hours, " _
& "Day_Shift_Extra_Hours,Month_Shift_Hours,Month_Shift_Extra_Hours,Month_Official_Hours,from_date,to_date,ShowRoom_col,Job,Year_No) " _
& "values (@emp_code,@Name_col,@Date_Col,@Shift_Col,@Mor_from,@Mor_to,@eve_from, " _
& "@eve_to,@BasicSalary_col,@Hour_Value_col2,@Over_Hours_col2, " _
& "@Trans_Value_col2,@Total_OverTime_col2,@Month_No,@create_user,@create_date,@create_time,@Ikama_No,@Official_Hours,@Day_Shift_Hours, " _
& "@Day_Shift_Extra_Hours,@Month_Shift_Hours,@Month_Shift_Extra_Hours,@Month_Official_Hours,@from_date,@to_date,@ShowRoom_col,@Job,@Year_No)", conn)
mycommand.Parameters.AddWithValue("@ShowRoom_col", Dawam_Grid.Items(Z).Cells("ShowRoom_col1").Value.ToString)
mycommand.Parameters.AddWithValue("@Job", Dawam_Grid.Items(Z).Cells("Job1").Value.ToString)
mycommand.Parameters.AddWithValue("@emp_code", Dawam_Grid.Items(Z).Cells("Emp_Code1").Value.ToString)
mycommand.Parameters.AddWithValue("@Name_col", Dawam_Grid.Items(Z).Cells("Emp_Name1").Value.ToString)
mycommand.Parameters.AddWithValue("@Date_Col", Dawam_Grid.Items(Z).Cells("Date_Col1").Value)
mycommand.Parameters.AddWithValue("@Shift_Col", Dawam_Grid.Items(Z).Cells("Shift_Col1").Value.ToString)
mycommand.Parameters.AddWithValue("@Mor_from", Dawam_Grid.Items(Z).Cells("Time_Mor_from1").Value)
mycommand.Parameters.AddWithValue("@Mor_to", Dawam_Grid.Items(Z).Cells("Time_Mor_to1").Value)
mycommand.Parameters.AddWithValue("@eve_from", Dawam_Grid.Items(Z).Cells("Time_eve_from1").Value)
mycommand.Parameters.AddWithValue("@eve_to", Dawam_Grid.Items(Z).Cells("Time_eve_to1").Value)
mycommand.Parameters.AddWithValue("@BasicSalary_col", Dawam_Grid.Items(Z).Cells("Salary_col1").Value)
mycommand.Parameters.AddWithValue("@Hour_Value_col2", Dawam_Grid.Items(Z).Cells("Hour_Value_col1").Value)
mycommand.Parameters.AddWithValue("@Over_Hours_col2", Dawam_Grid.Items(Z).Cells("Over_Hours_col1").Value)
mycommand.Parameters.AddWithValue("@Trans_Value_col2", Dawam_Grid.Items(Z).Cells("Trans_Value_col1").Value)
mycommand.Parameters.AddWithValue("@Total_OverTime_col2", Dawam_Grid.Items(Z).Cells("Total_OverTime_col1").Value)
mycommand.Parameters.AddWithValue("@Month_No", Dawam_Grid.Items(Z).Cells("Month_No1").Value)
mycommand.Parameters.AddWithValue("@Ikama_No", Dawam_Grid.Items(Z).Cells("Ikama_No1").Value)
mycommand.Parameters.AddWithValue("@create_user", Main_UserName)
mycommand.Parameters.AddWithValue("@create_date", DateTime.Today.Date.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))
mycommand.Parameters.AddWithValue("@create_time", DateTime.Now.ToString("HH:mm:ss"))
mycommand.Parameters.AddWithValue("@Official_Hours", Dawam_Grid.Items(Z).Cells("Official_Hours1").Value)
mycommand.Parameters.AddWithValue("@Day_Shift_Hours", Dawam_Grid.Items(Z).Cells("Day_Shift_Hours1").Value)
mycommand.Parameters.AddWithValue("@Day_Shift_Extra_Hours", Dawam_Grid.Items(Z).Cells("Day_Shift_Extra_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Official_Hours", Dawam_Grid.Items(Z).Cells("Month_Official_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Shift_Hours", Dawam_Grid.Items(Z).Cells("Month_Shift_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Shift_Extra_Hours", Dawam_Grid.Items(Z).Cells("Month_Shift_Extra_Hours1").Value)
mycommand.Parameters.AddWithValue("@from_date", Dawam_Grid.Items(Z).Cells("from_date1").Value)
mycommand.Parameters.AddWithValue("@to_date", Dawam_Grid.Items(Z).Cells("to_date1").Value)
mycommand.Parameters.AddWithValue("@Year_No", Format(From_Date.EditValue, ("yyyy")))
mycommand.ExecuteNonQuery()
Next
the error message :
System.MissingMemberException: cannot find 'Cells'in type 'Dawam_Grid_Items'.'
Dawam_Grid_Items
is a public class I created to add rows to the datagrid
Public Class Dawam_Grid_Items
Public Property Emp_Code1 As String
Public Property Emp_Name1 As String
Public Property Salary_col1 As String
Public Property Hour_Value_col1 As String
Public Property Over_Hours_col1 As String
Public Property Trans_Value_col1 As String
Public Property Total_OverTime_col1 As String
Public Property Ikama_No1 As String
Public Property ShowRoom_col1 As String
Public Property Time_Mor_from1 As String
Public Property Time_Mor_to1 As String
Public Property Time_eve_from1 As String
Public Property Time_eve_to1 As String
Public Property Date_Col1 As String
Public Property Official_Hours1 As String
Public Property Day_Shift_Hours1 As String
Public Property Day_Shift_Extra_Hours1 As String
Public Property Month_Official_Hours1 As String
Public Property Month_Shift_Hours1 As String
Public Property Month_Shift_Extra_Hours1 As String
Public Property Shift_Col1 As String
Public Property Month_No1 As String
Public Property from_date1 As String
Public Property to_date1 As String
Public Property Job1 As String
End Class