Hi, I'm new to database programming and I seem to be doing something wrong.
I have an application program where I use a datetimepicker to choose a date. I convert the selected date to a string, and then attempt to write it to one of the tables in my database. The table(weekEnding) has only two fields, the key field(WEID), and a string field(WeekEnding)
Here is the code I'm using:
Private Sub AcceptDateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcceptDateBtn.Click
Dim SQLString As String = "Insert INTO WeekEnding(WeekEnding) VALUES(SelectedWeekEnding)"
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand()
Dim SelectedWeekEnding As String
cmd.Connection = conn
cmd.CommandType = Data.CommandType.Text
cmd.CommandText = SQLString
SelectedWeekEnding = DateTimePicker1.Text.ToString
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
WeekEndingGrpBox.Visible = False
End Sub
When it comes to the ExecuteNonQuery I get the following exception
The name "SelectedWeekEnding" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Can someone please explain what I'm doing wrong or how to get the string written into the table?
I have an application program where I use a datetimepicker to choose a date. I convert the selected date to a string, and then attempt to write it to one of the tables in my database. The table(weekEnding) has only two fields, the key field(WEID), and a string field(WeekEnding)
Here is the code I'm using:
Private Sub AcceptDateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcceptDateBtn.Click
Dim SQLString As String = "Insert INTO WeekEnding(WeekEnding) VALUES(SelectedWeekEnding)"
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand()
Dim SelectedWeekEnding As String
cmd.Connection = conn
cmd.CommandType = Data.CommandType.Text
cmd.CommandText = SQLString
SelectedWeekEnding = DateTimePicker1.Text.ToString
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
WeekEndingGrpBox.Visible = False
End Sub
When it comes to the ExecuteNonQuery I get the following exception
The name "SelectedWeekEnding" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Can someone please explain what I'm doing wrong or how to get the string written into the table?