i have been trying to insert data from Excel file > Datatable > Dataset > into Sql table ( dbo.fingerprintslogs ) in database ( fingerprint reader )
i successfully inserted the data to the datatables and the data set . so now i have been trying to set the insert command in data adapter . My data table looks like the following :
Employee ID | Date | Weekday | First Check In | Last Check Out |
---|---|---|---|---|
10 | 2024-04-15 | Monday | 15:04 | |
10 | 2024-04-16 | Tuesday | 08:46 | 14:41 |
10 | 2024-04-17 | Wednesday | 08:34 | |
10 | 2024-04-18 | Thursday | 07:50 | 15:18 |
10 | 2024-04-20 | Saturday | 09:00 | 15:10 |
10 | 2024-04-21 | Sunday | 08:16 | 15:53 |
10 | 2024-04-22 | Monday | 08:41 | 15:22 |
10 | 2024-04-25 | Thursday | 08:05 | |
10 | 2024-04-27 | Saturday | 08:10 | |
10 | 2024-04-28 | Sunday | 07:27 | |
10 | 2024-04-29 | Monday | 07:57 | 16:04 |
10 | 2024-04-30 | Tuesday | 07:40 | 15:32 |
101 | 2024-04-02 | Tuesday | 08:55 | 10:54 |
i used this code in order to loop through all rows in a dataset.table(0) to use the sqlcommand :
Public conn As New SqlConnection("Server= DESKTOP-C1JI0OA\SQLEXPRESS; TrustServerCertificate=True; Encrypt=true; TrustServerCertificate=true; Trusted_Connection=True; Database = Fingerprint reader;")
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim query1 As String
Dim cmd As New SqlCommand(query1, conn)
ds.Tables.Add(dt1)
ds.Tables(0).TableName = "Table"
da.TableMappings.Add("Table", "dbo.fingerprintslogs")
conn.Open()
For Each DataTable As DataTable In ds.Tables
For Each dtrow In DataTable.Rows
Dim EmployeeID As String = dtrow(0)
Dim Date1 As String = dtrow(1)
Dim WeekDay As String = dtrow(2)
Dim Firstcheckin As String = dtrow(3)
Dim Lastcheckout As String = dtrow(4)
query1 = "INSERT INTO fingerprintslogs Values('" & EmployeeID & "','" & Date1 & "','" & WeekDay & "','" & Firstcheckin & "','" & Lastcheckout & "')"
cmd.Parameters.AddWithValue("@Emloyee ID", SqlDbType.Int)
cmd.Parameters.AddWithValue("@Date", SqlDbType.Date)
cmd.Parameters.AddWithValue("@WeekDay", SqlDbType.NVarChar)
cmd.Parameters.AddWithValue("@First check in", SqlDbType.Time)
cmd.Parameters.AddWithValue("@Last check out", SqlDbType.Time)
cmd.ExecuteNonQuery()
da.InsertCommand = cmd
da.Update(ds.Tables(0))
Next
Next
conn.Close()
I get error:
ExecuteNonQuery CommandText property has not been properly initialized
and at this point i'm not sure even if the way i'm using is correct x)
query1
is defined, but NOT initialized before your instantiation of SqlCommand
so you pass an empty string which results in the error you quoted. The SqlCommand
object will not be notified of theString
you passed to it being changed. Make sure you initialize your string, like:
Dim query1 As String = "INSERT INTO fingerprintslogs (Values('@EmployeeID','@Date1','@WeekDay','@Firstcheckin','@Lastcheckout')"
Dim cmd As New SqlCommand(query1, conn)
note that I added query parameters to your query instead of hard-coding your variables which is unsafe.
It also looks strange that you loop your datatables and each row of each datatable, maybe you wanted to loop a specific datatable instead, but I'm not sure about this.
Fix the parameter names you pass (I have removed the spaces from them):
cmd.Parameters.AddWithValue("@EmloyeeID", EmployeeID)
cmd.Parameters.AddWithValue("@Date1", Date1)
cmd.Parameters.AddWithValue("@WeekDay", WeekDay)
cmd.Parameters.AddWithValue("@Firstcheckin", Firstcheckin)
cmd.Parameters.AddWithValue("@Lastcheckout", Lastcheckout)
And make sure your white-spaces are correct, it does not make sense to have cmd
declaration being tabulated to the right in comparison with the declaration of query1
above it.
EDIT
As Andrew Morton pointed out in the comment-section, AddWithValue
may cause performance issues, so it is recommended to use something like this instead:
cmd.Parameters.Add("@EmloyeeID", SqlDbType.Int).Value = EmployeeID
cmd.Parameters.Add("@Date1", SqlDbType.Date).Value = Date1
cmd.Parameters.Add("@WeekDay", SqlDbType.NVarChar).Value = WeekDay
cmd.Parameters.Add("@Firstcheckin", SqlDbType.Time).Value = Firstcheckin
cmd.Parameters.Add("@Lastcheckout", SqlDbType.Time).Value = Lastcheckout
so that the type will not have to be inferred.