im getting this error when i changed my textbox to datetimepicker
here is my code
Dim cmd As SqlCommand = sqlconn.CreateCommand
sqlconn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AddOfficeEquipmentProfile"
cmd.Parameters("#OE_PurchaseDate").Value = dtpPurchaseDate.Value
cmd.ExecuteNonQuery()
sqlconn.close
this is my stored procedure as follows
CREATE PROCEDURE AddOfficeEquipmentProfile
(
#OE_PurchaseDate smalldatetime,
)
AS
INSERT INTO tblOfficeEquipmentProfile (OE_PurchaseDate)
VALUES (#OE_PurchaseDate)
GO
I think the error message is quite straightforward. You need to add the parameter before attempting to set its value.
cmd.CommandText = "AddOfficeEquipmentProfile"
cmd.Parameters.Add("#OE_PurchaseDate",SqlDbType.SmallDateTime)
cmd.Parameters("#OE_PurchaseDate").Value = dtpPurchaseDate.Value
Related
Dim sc = datagridview.rows(0).cells(0).value
cmd = New SqlCommand("insert into Schedule (Name) values(#sc)", con)
with cmd
.Connection = con
.CommandType = CommandType.Text
.Parameters.AddWithValue("#sc", sc.cells(0))
end with
The value from the datagrid view contains an apostrophe where I get an error in ('s). Any work around with this?
EDIT:
This is the error:
SqlException was unhandled. Incorrect syntax near 's'.
EDIT 2: i've revised the code to this:
for 1=0 to datagridview.rows.count -1
Dim tname = datagridview.Rows(i)
cmd = New SqlCommand("insert into Schedule (Name) values(#sc)", con)
with cmd
.Connection = con
.CommandType = CommandType.Text
.Parameters.AddWithValue("#sc", sc.cells(0).value)
end with
con.Close()
con.Open()
If Not cmd.ExecuteNonQuery() > 0 Then
con.Close()
Exit For
End If
next
Error is still the same.
As it goes through all the values quickly in one go, I would make the connection once and change the value of the parameter for each iteration, like this:
Dim sql = "INSERT INTO [Schedule] ([Name]) VALUES(#sc)"
Using conn As New SqlConnection("your connection string"),
cmd As New SqlCommand(sql, conn)
cmd.Parameters.Add(New SqlParameter With {.ParameterName = "#sc",
.SqlDbType = SqlDbType.NVarChar,
.Size = -1})
conn.Open()
For i = 0 To dgv.Rows.Count - 1
Dim tname = dgv.Rows(i).Cells(0).Value.ToString()
cmd.Parameters("#sc").Value = tname
cmd.ExecuteNonQuery()
Next
End Using
Including the size of the parameter in its declaration helps SQL Server keep things tidy (it only needs one entry in the query cache).
The Using statement makes sure that the connection and command are disposed of properly when they are finished with.
The purpose of checking If Not cmd.ExecuteNonQuery() > 0 was not clear to me, so I didn't put that in.
(I used "dgv" as the name of the DataGridView.)
Try to replace the " ' " by " '' "
Dim sc = datagridview.rows(0).cells(0).value
cmd = New SqlCommand("insert into Schedule (Name) values(#sc)", con)
with cmd
.Connection = con
.CommandType = CommandType.Text
.Parameters.AddWithValue("#sc", replace(sc.cells(0), "'", "''")
end with
/**** EDIT ****/
I will consider by the way that your code have a problem with the affectation of the value. You get the value into SC then try to get again the value from cell
Dim sc = datagridview.rows(0).cells(0).value
cmd = New SqlCommand("insert into Schedule (Name) values(#sc)", con)
with cmd
.Connection = con
.CommandType = CommandType.Text
.Parameters("#sc").value = sc.replace("'", "''")
end with
Im getting this error while inserting data to my mdf database file
System.IndexOutOfRangeException: 'An SqlParameter with ParameterName '#qtysold' is not contained by this SqlParameterCollection.'
This is my code for order function
Function order(ByVal cart As List(Of Integer), ByVal product_qty As List(Of Integer))
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
'INSERT INTO PRODUCT TABLE
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE product SET product_qty= product_qty - #qty WHERE product_id= #id"
cmd.Parameters.Add("#qty", SqlDbType.Int)
cmd.Parameters.Add("#id", SqlDbType.Int)
For i As Integer = 0 To cart.Count - 1
cmd.Parameters("#id").Value = cart(i)
cmd.Parameters("#qty").Value = product_qty(i)
cmd.ExecuteNonQuery()
Next
'INSERT INTO SALES TABLE
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO sales VALUES (#id, #productname, #qtysold, #date)"
cmd.Parameters.Add("#id", SqlDbType.Int)
cmd.Parameters.Add("#productname", SqlDbType.VarChar)
cmd.Parameters.Add("#qtysold", SqlDbType.Int)
cmd.Parameters.Add("#date", SqlDbType.Date)
For i As Integer = 0 To cart.Count - 1
cmd.Parameters("#id").Value = cart(i)
cmd.Parameters("#productname").Value = getName(cart(i))
cmd.Parameters("#qtysold").Value = product_qty(i)
cmd.Parameters("#date").Value = datefield.Text
cmd.ExecuteNonQuery()
Next
cart.Clear()
product_qty.Clear()
refresh()
End Function
THE ERROR HAPPENS HERE
cmd.Parameters("#qtysold").Value = product_qty(i)
That seems an odd exception in that situation as the code looks OK to me but it's wrong even if it did work. Don't add the parameters and then get them back by name over and over. The Add method you're calling returns the SqlParameter object added so assign it to a variable and then use that:
Dim idParameter = cmd.Parameters.Add("#id", SqlDbType.Int)
Dim productionNameParameter = cmd.Parameters.Add("#productname", SqlDbType.VarChar)
Dim qtySoldParameter = cmd.Parameters.Add("#qtysold", SqlDbType.Int)
Dim dateParameter = cmd.Parameters.Add("#date", SqlDbType.Date)
For i As Integer = 0 To cart.Count - 1
idParameter.Value = cart(i)
productionNameParameter.Value = getName(cart(i))
qtySoldParameter.Value = product_qty(i)
dateParameter.Value = datefield.Text
cmd.ExecuteNonQuery()
Next
NEVER use the same complex expression over and over. ALWAYS use it once and assign the result to a variable, then use that variable over and over. In this case, you don't even have to use the complex expression once, because you already had the object but just ignored it.
I want to display the date column into a textbox that a stored procedure returns. The error displays the column name.
Thank you for your help!
SQL stored procedure:
SELECT MAX(NrTimesUpdated) AS NrTimesUpdated,
CONVERT(varchar(20),MAX(DateModified),101) AS DateEntered
FROM Credentials
WHERE ID = #ID
END
VB code:
Dim cmd As New SqlClient.SqlCommand
cmd = New SqlCommand("get_credentials", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = lblID.Text
Dim rdr = cmd.ExecuteReader
If rdr.Read Then
lblDate.Text = rdr("DateEntered").ToString().Trim()
End If
rdr.Close()
i had assigned a job for insert,update, delete operation in a table for my student. he had done this assignment fine. but his code is made me crazy that he wrote update code like below
Working sample:
Public Function Toeditdetails(ByVal item As Boolean)
If item = True Then
Dim con As SqlConnection
'Dim retval As Integer
con = New SqlConnection(conn)
con.Open()
Dim cmd As SqlCommand
cmd = New SqlCommand("updatelogin", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("#username", SqlDbType.VarChar, 100)).Value() = TextBox1.Text.Trim()
cmd.Parameters.Add(New SqlParameter("#user_password", SqlDbType.VarChar, 30)).Value() = TextBox2.Text.Trim()
cmd.Parameters.Add(New SqlParameter("#nameofuser", SqlDbType.VarChar, 100)).Value() = TextBox3.Text.Trim()
cmd.Parameters.Add(New SqlParameter("#contact_no", SqlDbType.VarChar, 10)).Value() = TextBox4.Text.Trim()
cmd.Parameters.Add(New SqlParameter("#email", SqlDbType.VarChar, 100)).Value() = TextBox5.Text.Trim()
cmd.Parameters.Add(New SqlParameter("#joiningdate", SqlDbType.Date)).Value() = DateTimePicker1.Value
cmd.Parameters.Add(New SqlParameter("#usergroup_id", SqlDbType.Int)).Value() = SelectedID(ComboBox1.Text)
If item = True Then
cmd.Parameters.Add(New SqlParameter("#id", SqlDbType.Int)).Value() = Label9.Text.Trim()
End If
Dim da As SqlDataAdapter
da = New SqlDataAdapter(cmd)
Dim dst As New DataSet
da.Fill(dst)
If dst.Tables.Count > 0 Then
DataGridView1.DataSource = dst.Tables(0)
End If
If Not cmd Is Nothing Then
cmd.Dispose()
End If
End If
End Function
Note there is no ExecuteNonQuery() method for his SqlCommand object and he didn't call this method in entire project. when i call function result still updating... how its happen? anyone genius can you explain whats going on above statement? any help would be appreciated
Regards and thanks
Sarva
when you call Toeditdetails method it will execute stored procedure called updatelogin
check the stored procedure and you may able to find why you receive updated result. Stored procedure several statements like below
update table1 set username ='test1' where uid =#uid;
select * from table1;
when you call stored procedure with above code you will get the table1 data but update statement will update data before you receive it.
Dim cmd As SqlCommand = sqlconn.CreateCommand
sqlconn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "DeletetblOfficeEquipmentProfileRecord"
cmd.Parameters.Add("#OEID", SqlDbType.VarChar, 11, "oeq-su-999")
cmd.Parameters("#OEID").Value = "oeq-su-999"
cmd.ExecuteNonQuery()
sqlconn.Close()
above is my stored procedure how can I assign txtOEID.text a textbox
to serve as the value for #OEID parameter?
my stored procedure is delete columns WHERE OEID value in textbox
how can I assign txtOEID.text a textbox to serve as the value for
#OEID
Currently you are assigning it a string, replace it with your txtOEID.Text property like:
cmd.Parameters("#OEID").Value = txtOEID.Text
Try this:
variable_name=textbox1.text
command.Params.Add("#OEID", SqlDbType.VarChar, 11, variable_name)