[英]Working on a VB.net program with MS SQL as my database. Then this error popped-up when I'm trying to insert another value into my table
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Main
Dim c As New SqlCommand
Dim connection As String = "Server=DESKTOP-7MC233A\SQLJOSE;database=AttendanceLog;user id=sa;password=pogi1234;"
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Dim dtmNow As DateTime = System.DateTime.Now
If dtmNow.Hour > 0 And dtmNow.Hour < 11 Then
Label2.Text = "Good morning"
ElseIf dtmNow.Hour > 12 And dtmNow.Hour < 18 Then
Label2.Text = "Good afternoon"
ElseIf dtmNow.Hour > 19 And dtmNow.Hour < 23 Then
Label2.Text = "Good evening "
Else
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
lblDate.Text = Date.Now.ToString("MMM dd,yyyy")
lblTime.Text = Date.Now.ToString("hh:mm:ss")
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles lblDate.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbNames.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using con As New SqlConnection(connection)
Try
If con.State = ConnectionState.Closed Then
con.Open()
End If
With c
.Connection = con
.CommandText = "Insert into TimeIn values(@Name,@TimeIn,@Date)"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@Name", cmbNames.Text)
.Parameters.AddWithValue("@TimeIn", lblTime.Text)
.Parameters.AddWithValue("@Date", lblDate.Text)
.ExecuteNonQuery()
End With
Dim dtmNow As DateTime = System.DateTime.Now
If dtmNow.Hour > 9 Then
MsgBox("You are late!!!", MsgBoxStyle.Critical)
ElseIf dtmNow.Hour < 9 Then
MsgBox("You are on time!", MsgBoxStyle.Information)
End If
Catch ex As Exception
MsgBox("From: " & ex.Message)
End Try
con.Dispose()
End Using
End Sub
End Class
Here are my codes, and I'm very lost to what I'm going to do next. 这是我的代码,我对接下来要做的事情非常迷茫。 Thanks in advance for the help. 先谢谢您的帮助。
Just Dispose you sqlCommand every after using it. 只需在每次使用后处置sqlCommand。
you can use: 您可以使用:
Using c As New SqlCommand("commandtext", connection")
Or you can just.. 或者你可以..
With c
'after the execution
.Parameters.Clear()
.Dispose
End With
Create a new sqlcommand every time. 每次都创建一个新的sqlcommand。 The same way you create a new Connection every time. 每次创建新连接的方式相同。
The problem you have is that you're adding @name again on the second pass. 您遇到的问题是您在第二遍再次添加了@name。
Using con As New SqlConnection(connection)
Using cmd as New SqlCommand(...)
....
End Using
End Using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.