ASP.net SQL database amend existing string field data using command parameters - asp.net

I am trying to maintain a log field (fldUserLog ) of my database table so that when updating each raw, the log field will be amended with given log string.
Log string
strUserLog = "Added by : " & Session("auth_Id") & " at " & Now() & " from IP " &
Request.ServerVariables("REMOTE_ADDR") & vbCrLf
and I am using SQL command parameters to UPDATE query. as follows
strSQLQuery = "UPDATE myTable SET " _
& "fldTitle = #xTitle, " _
& "fldDesc = #xDesc, " _
& "fldUserLog = fldUserLog + #xUserLog " _
& "WHERE fldId = #xId ;"
strMessage = "updated"
ObjAddDB.setCommand(strSQLQuery)
With ObjAddDB
.setParameters("#xTitle", frmTitle.Text)
.setParameters("#xDesc", frmDesc.Text)
.setParameters("#xUserLog", strUserLog)
.setParameters("#xId", MyItemId)
End With
Please note that setCommand and setParameters are my own methods I am using in my database.vb class file.
I get following error when its executed
Exception Details:
System.Data.SqlClient.SqlException:
Incorrect syntax near 'fldUserLog'.
please help me to use my UPDATE query to amend existing data with command parameters.

If the format of the fldUserLog field value contains spaces, you need to embrace the value with [ ] ..
& "fldUserLog = [fldUserLog #xUserLog] " _
I guess what you may want to write is the following:
& "fldUserLog = #xUserLog " _

Related

update data where IN list asp.net

Dim nilx As String = textbox1.text --> (example. data like this. "10001,010001")
Using koneksi As New SqlConnection(stringkoneksi)
koneksi.Open()
Dim perintah As SqlCommand = koneksi.CreateCommand
perintah.CommandText = "update mytabledata set rumus='" & TextBox2.Text & "'
where code in (" & nilx & ") "
perintah.CommandType = CommandType.Text
perintah.ExecuteNonQuery()
koneksi.Close()
error when execute
Conversion failed when converting the nvarchar value '123test' to data type int
because in mytabledata there is code 123test, if i delete code 123test running well
so how to update data where code in (" & nilx & ")
error when execute
Conversion failed when converting the nvarchar value '123test' to data type int
because in mytabledata have code 123test, if i delete code 123test running well
so how to update data where code in (" & nilx & ")
if i have data nilx = "10001,010001,9912AA,1123BC"
Your codes in nilx must be in single quotes:
"'10001','010001','9912AA','1123BC'"
Also, use parameterized queries SQL Injection And Parameterized Queries. Don't just concatenate TextBox2.Text in your commands.

What is wrong with the Search Form codes?

I have a Search Form which have a sub form in it.
I keep getting Run-time error '3075': Syntax error(missing operator) in query expression '[MC_No] like '' [Customer] like '' [Date_Recorded] = #23/11/2016# AND [Product] like '*".
Can anybody identify my error? I have checked every single line. Maybe I missed out on something.
These are my codes
Private Sub Search_Click()
Dim strDatePicker As String
Dim cboMC As String
Dim strProduct As String
Dim cboCustomer As String
Dim sql As String
sql = "select * FROM 3_OEE WHERE "
If IsDate(Me.DatePicker) Then
strDatePicker = " [Date_Recorded] = #" & DateValue(Me.DatePicker) & "#"
Else
strDatePicker = " [Date_Recorded] like '*'"
End If
If IsNull(Me.MC_No) Then
cboMC = " [MC_No] like '*'"
Else
cboMC = " [MC_No] like '" & Me.MC_No & "'"
If IsNull(Me.Customer) Then
cboCustomer = " [Customer] like '*'"
Else
cboCustomer = " [Customer] = '" & Me.Customer & "'"
End If
Me.subfrmOEE.Form.RecordSource = sql (*Error highlights this code*)
Me.subfrmOEE.Form.Requery
On the line with the error, your variable sql is the same as it was when you first declared it, i.e.: "select * FROM 3_OEE WHERE ".
This is not a valid query. You need to either remove WHERE from the end of that query or append to the sql variable the rest of the query statement (in other words, add the WHERE conditions to the end of the query).

Entering each row of datagrid into SQL database

I'm attempting to take a column from each row of my datagrid and enter it into an SQL database. Having all sorts of problems/errors making it work.
This is the most current version:
For i = 0 to agentGridView.Rows.Count - 1
varAgt = agentGridView.Rows(i).Cells(1).Text
strSQL = "INSERT INTO tblAgentVisitAgents (VisitID, AgtID)" & _
" Values (" & _
VisitID.Text & "," & _
varAgt & ")"
strSQL = Utils.replaceChars(strSQL)
DAL.ExecNonQuery(strSQL, CommandType.Text)
Next
EDIT: The issue is that my cell(1) is a hidden field. When I make it visible, the entry form works. When it's hidden, it won't enter anything and thus gives me a syntax error. Is there a way I can use a hidden field for entry purposes?
You need to use Text instead of Value . Like following.
varAgt = agentGridView.Rows(i).Cells(1).Text instead of varAgt = agentGridView.Rows(i).Cells(1).Value
But if you have that control in a Label, then you need to typecast to label and then use Text. E.g.
varAgt = CType(agentGridView.Rows(i).Cells(1).FindControl("controlID"),Label).Text -- replace controllID with required label id.
Source - http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx
Here's what I went with:
Front:
<asp:GridView ID="agentGridView" DataKeyNames="agentName,agentValue" ... />
Back:
For i = 0 to agentGridView.Rows.Count - 1
varAgt = agentGridView.DataKeys(i).Values("agentValue").ToString
strSQL = "INSERT INTO tblAgentVisitAgents (VisitID, AgtID)" & _
" Values (" & _
VisitID.Text & "," & _
varAgt & ")"
strSQL = Utils.replaceChars(strSQL)
DAL.ExecNonQuery(strSQL, CommandType.Text)
Next
Source (for C#): Hide BoundField's but still be able to get values with C#

conversion from string to type 'double' is not valid in LINQ query

I'm getting a 'conversion from string to type 'double' error in the following Linq query:
Dim query = From Corrections In Test _
Where Not Corrections.Expected_Bill = Corrections.Bill _
Select Corrections.Account, _
Corrections.Site, _
Action = If(Corrections.Bill = "No billing PLI", _
+"This site has no billing PLI set up; " _
+ "please add " + Corrections.Expected_Bill, _
+"This site has an incorrect billing PLI set up; " _
+ "please remove " + Corrections.Bill + " and " _
+ "add " + Corrections.Expected_Bill)
It looks like the 'If(Corrections.Bill = "No billing PLI"' part of the query is the one throwing the exception but given that all fields are text I don't understand why this would be happening. Any ideas? Thanks!
You have an extra + after comma. Replace
+"This site has an incorrect billing PLI set up; " _
with
"This site has an incorrect billing PLI set up; " _

Parsing Date from VB.NET for SQL INSERT

I have a web app with a form that I am trying to pass to an ASP.NET server (using VB.NET) and then on to a MS SQL Server table. The form uses a jQuery datepicker in several textboxes and formats them as MM/dd/yyyy. The form fields are then passed through a PageMethod to the web server which takes the various field values and combines them into a SQL UPDATE command.
I am constantly getting the following error whenever I try to execute the SQL command:
Conversion failed when converting date and/or time from character string.
Here is the code on the server:
Using myConn As New System.Data.SqlClient.SqlConnection(CString.ConnectionString)
myConn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = '" & "#target" & "', " & _
"patient = '" & patient & "', " & _
"dob = '" & "#dob" & "' " & _
"WHERE serial = '" & serial & "'", myConn)
cmd.Parameters.Add(SqlParameter("#target", Data.SqlDbType.Date))
cmd.Parameters.Add(SqlParameter("#dob", Data.SqlDbType.Date))
If target = "" Then
cmd.Parameters("#target").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#target").Value = target
End If
If dob = "" Then
cmd.Parameters("#dob").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#dob").Value = dob
End If
cmd.ExecuteNonQuery()
End Using
Note: I've tried about twenty different ways of parsing the dates, converting them to dates, changing around the formats and none of it has worked.
Note 2: The conditional statements at the end are simply to prevent empty date fields from being stored in the SQL DB as "1/1/1900", but rather as an actual SQL NULL value. From debugging though, it seems that this is not the issue - it is when there is an actual value that the error is fired.
If anyone can see what I'm doing wrong and how I might fix it, it would be greatly appreciated. Thanks in advance for your help!
You are mixing up your parameterized and non-parameterized parts (why aren't you parameterizing everything?)
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = #target, " & _
"patient = '" & patient & "', " & _
"dob = #dob " & _
"WHERE serial = '" & serial & "'", myConn)
Are you including time? DateTime fields require date and time.

Resources