Wondering if someone can help me with this please. I'm getting the following error message when adding data to an access database.I know it's a time issue function but I don't know how to fix it so I can add data to the database in the required format. One or more values are prohibited by the validation rule 'Time()' set for 'tblmph'. Enter a value that the expression for this field can accept.
Public Sub Add_Data()
con.Open()
Dim rs As New OleDb.OleDbCommand("Insert into tblmph(ID,ThisDate,TimeStart,TimeFinish,Notes) " _
& "values ('" & TextBox1.Text & "' , '" & TextBox2.Text & "' , '" & TextBox3.Text & "' , '" _
& TextBox4.Text & "', '" & TextBox5.Text & "')", con)
rs.ExecuteNonQuery()
con.Close()
Display_Data()
End Sub
The columns are formatted as follows
ID = Auto Number-Long Integer
ThisDate = Short Date
TimeStart = Medium Time, Default Value = Time()
TimeFinish = Medium Time, Default Value = Time()
Notes = Memo
Do not insert the value of ID. If it is truly an auto-number, Access will automatically assign a value to the newly inserted row. Trying to insert a value into the field will cause an error and prevent the insert from working.
Change your Dim statement to:
Dim rs As New OleDb.OleDbCommand( "Insert into tblmph( ThisDate, TimeStart, TimeFinish, Notes ) values ( '" & TextBox2.Text & "' , '" & TextBox3.Text & "' , '" & TextBox4.Text & "', '" & TextBox5.Text & "' ) ", con)
Related
I'm working on a vb.net project, which connected with sql 2008 server. Where I need to saved data from datagridview. It's work well for first time. After saved one data I just clear the datagridview and input new data on that datagridview. But in this time when I start the process for saving new data from datagridview to sql it show the error:
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index
If con.State = 1 Then con.Close()
con.Open()
Using Debit_Product_ListCmd_CHK As New SqlClient.SqlCommand("select *from Debit_Product_List where DEBIT_NOTE_No='" & Trim(UCase(ProductCart.Rows(C).Cells(7).Value)) & "' and DEBIT_NOTE_DATE='" & Trim(UCase(ProductCart.Rows(C).Cells(8).Value)) & "' and ShopGSTNO='" & _
Trim(UCase(ProductCart.Rows(C).Cells(9).Value)) & "' and SupplierGSTNO='" & Trim(UCase(ProductCart.Rows(C).Cells(10).Value)) & "' and PRODUCT_CATEGORY='" & Trim(UCase(ProductCart.Rows(C).Cells(11).Value)) & "' and PRODUCT_SLNO='" & _
Trim(UCase(ProductCart.Rows(C).Cells(0).Value)) & "' and ProductDesc=#ProductDesc", con)
Debit_Product_ListCmd_CHK.Parameters.AddWithValue("#ProductDesc", Trim(UCase(ProductCart.Rows(C).Cells(1).Value)))
Dim readerRecordShowcmd As SqlDataReader = Debit_Product_ListCmd_CHK.ExecuteReader()
If readerRecordShowcmd.Read() Then
'Sale_Production_CUST_INVOICE_SLNO = ""
If rsdl.State = 1 Then rsdl.Close()
rsdl.Open("delete from Debit_Product_List where DEBIT_NOTE_No='" & Trim(UCase(ProductCart.Rows(C).Cells(7).Value)) & "' and DEBIT_NOTE_DATE='" & Trim(UCase(ProductCart.Rows(C).Cells(8).Value)) & "' and ShopGSTNO='" & _
Trim(UCase(ProductCart.Rows(C).Cells(9).Value)) & "' and SupplierGSTNO='" & Trim(UCase(ProductCart.Rows(C).Cells(10).Value)) & "' and PRODUCT_CATEGORY='" & Trim(UCase(ProductCart.Rows(C).Cells(11).Value)) & "' and PRODUCT_SLNO='" & _
Trim(UCase(ProductCart.Rows(C).Cells(0).Value)) & "'", conn)
If rsdl.State = 1 Then rsdl.Close()
rsdl.Open("delete from Debit_Product_TOTAL where DEBIT_NOTE_No='" & Trim(UCase(ProductCart.Rows(C).Cells(7).Value)) & "' and DEBIT_NOTE_DATE='" & Trim(UCase(ProductCart.Rows(C).Cells(8).Value)) & "' and ShopGSTNO='" & _
Trim(UCase(ProductCart.Rows(C).Cells(9).Value)) & "' and SupplierGSTNO='" & Trim(UCase(ProductCart.Rows(C).Cells(10).Value)) & "' and PRODUCT_CATEGORY='" & Trim(UCase(ProductCart.Rows(C).Cells(11).Value)) & "'", conn)
End If
End Using
con.Close()
I expect it will work as well as first data saved, but it show the error.
Im updating an entry in MS Access using this code but it always returns an error. I dont know which part to correct. Please help me i cant seem to find which part is the error
Private Sub SaveChanges()
con.ConnectionString = OpenDBConnection()
con.Open()
Dim sql As String = "Update [JobOrderProd] set [CustomerOrderNumber] =
'" & CustomerOrderNumtxtbox.Text & "', [ItemNumber] = '" &
ItemNumbertxtbox.Text & "', [JerseyName] = '" &
JerseyNametxtbox.Text & "', [JerseyNumber] = '"`
& JerseyNumbertxtbox.Text & "', [JerseySize] = '" &
JerseySizetxtbox.Text & "',
[ShortsSize] = '" & ShortsSizetxtbox.Text
Dim cmd As New OleDbCommand(sql, con)`enter code here`
cmd.ExecuteNonQuery()`enter code here`
con.Close()
MsgBox("Entry for JO Production has been updated.")
ClearForm()
End Sub
I think you missed the single inverted comma in the end. add this to the end & "'"
JerseySizetxtbox.Text & "',
[ShortsSize] = '" & ShortsSizetxtbox.Text & "'"
So I was trying to do Update my database using buttons in VB.Net. I tried following different syntax in Updating tables but it still come up with the same error. It is so frustrating because we are running out of time to finish our system because of this. Please help me :'(
So this is my code wherein it will called after the button is clicked. What is wrong with my syntax here?
Public Sub UpdateClient(Client_ID As Integer, _ClientName As String, _Company_Add As String, _Email_Add As String,
_Tin_No As String, _Contact_Person As String, _Mobile_No As String, _Telephone_No As String,
_Remarks As String, _User As String)
Try
Dim strInsert As String = "UPDATE CLIENTS SET (ClientID = '" & Client_ID & "', ClientName = '" & _ClientName & "', Company_Add = '" & _Company_Add & "', Email_Add = '" & _Email_Add & "', Tin_No = '" & _Tin_No & "', Contact_Person = '" & _Contact_Person & "', Mobile_No = '" & _Mobile_No & "', Telephone_No = '" & _Telephone_No & "', Remarks = '" & _Remarks & "', User_ = '" & _User & "') WHERE (ClientID = '" & Client_ID & "') "
SQLCon.Open()
SqlCmd = New SqlCommand(strInsert, SQLCon)
SqlCmd.ExecuteNonQuery()
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Then here is my code on the button event:
Public Sub Update_Client()
SQL.UpdateClient(ClientIDLabel1.Text, txtCnamee.Text, txtCadd.Text, txtEadd.Text, txtTin.Text, txtCper.Text, txtMno.Text, txtTel.Text, txtRem.Text, User_Label1.Text)
End Sub
I'm pretty sure the error is in my sql string. What could it be? Please please help me :'(
P.s. I'm new to using VB.Net. Please bear with me :( Thank you.
Here is the immediate problem with your code: the syntax of UPDATE is as follows:
UPDATE <table> SET <field1>=<value1>, <field2>=<value2> ...
Note that the list of variables that you set is not enclosed in parentheses. You need to remove ( and ) from your parenthesized list to fix the syntax problem in your SQL:
Dim strInsert As String = "UPDATE CLIENTS SET ClientID = '" & Client_ID & "', ClientName = '" & _ClientName & "', Company_Add = '" & _Company_Add & "', Email_Add = '" & _Email_Add & "', Tin_No = '" & _Tin_No & "', Contact_Person = '" & _Contact_Person & "', Mobile_No = '" & _Mobile_No & "', Telephone_No = '" & _Telephone_No & "', Remarks = '" & _Remarks & "', User_ = '" & _User & "' WHERE (ClientID = '" & Client_ID & "') "
However, there is a much bigger problem: your program can be broken by =SQL injection attacks, which is very dangerous. You need to rewrite your SQL to accept parameters, and use parameterized SQL to fix this vulnerability. See an answer to this question for a quick example of how to parameterize your query.
Hello I'm coding a query that finds the shift if the date is correct and the username of the shift employee is selected. Usually there would be 7 labels for each day of the week but I narrowed it down for the sake of getting this part of the code to work, I've been stuck for an hour or so. Here is my code:
Dim strQuery as String = "SELECT [Time] FROM [ShiftDetails]" +
" WHERE ([Day] = '" & Label2.Text & "' & [Username] = '" & TextBox1.Text & "') " +
"Or ([Day] = '" & Label3.Text & "' & [Username] = '" & cboUser.Text & "')"
myCommand = New OleDbCommand(strQuery, myConnection)
Or here it is with colored syntax
http://prntscr.com/5oo6wz
When I did breakpoints I saw that label is the actual date and text is the username so theres nothing wrong with the label/textbox I dont think
don't use & for join conditions, use AND
myCommand = New OleDbCommand("SELECT DISTINCT [Time] FROM [ShiftDetails] WHERE ([Day] = '" & Label2.Text & "' AND [Username] = '" & TextBox1.Text & "') Or ([Day] = '" & Label3.Text & "' AND [Username] = '" & cboUser.Text & "')", myConnection)
Also I hope this is your test query and not the real one as it is prone to sql injection.
I have a VB.Net form which allows the user to update the customer details such as name, contact no:, etc. So when the customer enters the new name for the customer name etc. the application should update the corresponding field in the existing entry that relates to the customer ID.
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter
Dim dt As New DataTable
cn.ConnectionString = ("Data Source=NIMO-HP\SQLEXPRESS;Initial Catalog=FYP_db;Integrated Security=True")
cmd.Connection = cn
cn.Open()
cmd.CommandText = " UPDATE TblCustomerDetails (compID, compName, compContact, compAddress, compFax, compEmail, compPayterm, compTaxscheme, compPaymode, compRemarks ) SET Values ('" & lblCID.Text & "', '" & txtCname.Text & "', '" & txtCpno.Text & "', '" & txtCaddrs.Text & "','" & txtCfax.Text & "', '" & txtCemail.Text & "', '" & cmbPterm.Text & "','" & cmbTaxschm.Text & "',' " & cmbPmode.Text & "', '" & txtRemarks.Text & "') WHERE compID = '" & lblCID.Text & "';"
cmd.ExecuteNonQuery()
MsgBox("Account updated!!", MsgBoxStyle.Information, "Updation complete")
Your using a INSERT syntax for your UPDATE statement. Your UPDATE statement should have the form:
UPDATE tableName
SET col1 = val1,
col2 = val2,
col3 = val3
WHERE someColumn = someValue
Additionally, you are wide open to SQL Injection attacks by using non-parameterized queries. Finally, I would use a Using blocks to ensure your connection and command are properly closed and disposed of.
Putting it all together it would look something like this:
Using Dim cn As SqlConnection = New SqlConnection("Data Source=NIMO-HP\SQLEXPRESS;Initial Catalog=FYP_db;Integrated Security=True")
cn.Open()
Dim sqlQuery As String = "UPDATE TblCustomerDetails " + _
"SET compName = #compName, " + _
"compContact = #compContact, " + _
"compAddress = #compAddress, " + _
"compFax = #compFax, " + _
"compEmail = #compEmail, " + _
"compPayterm = #compPayterm, " + _
"compTaxscheme = #compTaxscheme, " + _
"compPaymode = #compPaymode, " + _
"compRemarks = #compRemarks " + _
"WHERE compID = #compID"
Using Dim cmd As SqlCommand = New SqlCommand(sqlQuery, cn)
cmd.Parameters.AddWithValue("#compFax", txtCname.Text)
cmd.Parameters.AddWithValue("#compContact", txtCpno.Text)
cmd.Parameters.AddWithValue("#compAddress", txtCaddrs.Text)
cmd.Parameters.AddWithValue("#compFax", txtCfax.Text)
cmd.Parameters.AddWithValue("#compEmail", txtCemail.Text)
cmd.Parameters.AddWithValue("#compPayterm", cmbPTerm.Text)
cmd.Parameters.AddWithValue("#compTaxscheme", cmbTaxschm.Text)
cmd.Parameters.AddWithValue("#compPaymode", cmbPmode.Text)
cmd.Parameters.AddWithValue("#compRemarks", txtRemarks.Text)
cmd.Parameters.AddWithValue("#compID", lblCID.Text)
Dim result As Integer
result = cmd.ExecuteNonQuery()
If result = 1 Then
MsgBox("Account updated!!", MsgBoxStyle.Information, _
"Updation complete")
Else
MsgBox("Account not updated!!", MsgBoxStyle.Information, _
"Updation not complete")
End If
End Using
End Using
There are a few more things to note in the above code sample:
First, I removed compID from the list of values to update. You're using that in your WHERE query, so I think you would have interesting results in your query if you're trying to update the same column you are using as part of your WHERE clause. Additionally, the source for that value is a Label, which tells me it's not supposed to be changed.
Secondly, ExecuteNonQuery() returns an int with the number of rows affected. In this case, it should be 1 - if it's not 1, I have you show a different message box.
Thirdly, cmbPTerm, cmbTaxxshm and cmbPmode sound like ComboBox to me, and you're not going to get what I think you're expecting using their Text property. I think you'll want SelectedText - hard to say without knowning how your ComboBoxes are bound. I'll leave that as an exercise for you :)
Fourth, I broke the UPDATE query up across several lines simply for readability - you don't have to do it that way, as long as the query is correct.
Finally, I'd suggest using MessagBox.Show() vs MsgBox.
Dim cnn As New SqlConnection
Dim cmd As New SqlCommand
cnn.ConnectionString = ("Data Source=NIMO-HP\SQLEXPRESS;Initial Catalog=FYP_db;Integrated Security=True")
cmd.Connection = cnn
cnn.Open()
cmd.CommandText = "update TblCustomerDetails set compName='" & txtCname.Text & "' , compContact = '" & txtCpno.Text & "' , compAddress = '" & txtCaddrs.Text & "' , compFax = '" & txtCfax.Text & "' , compEmail = '" & txtCemail.Text & "' , compPayterm = '" & cmbPterm.Text & "' , compTaxscheme = '" & cmbTaxschm.Text & "' , compPaymode = '" & cmbPmode.Text & "' , compRemarks = '" & txtRemarks.Text & "' where compID = '" & lblCID.Text & "'"
cmd.ExecuteNonQuery()
cnn.Close()
MessageBox.Show("entry updated!!!")