And I'm getting the error of "Syntax error in INSERT INTO statement".
When I directly run the insert statement (which I get from this code) in the access database, the record gets added without any error.
There is one more column of ID which is an autonumber field so I'm not taking it here.
Code: vb
Protected Sub AddNewSQLCode(ByVal sender As Object, ByVal e As EventArgs)
Dim strOwner As String = Chr(34) & Session("loginname") & Chr(34)
Dim strNodeText As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewNode"), TextBox).Text & Chr(34)
Dim strSQLCode As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewSQL"), TextBox).Text & Chr(34)
Dim strParam As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewParam"), TextBox).Text & Chr(34)
Dim strComment As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewComm"), TextBox).Text & Chr(34)
Try
strSql = "insert into myreports(Owner, Node, IsTree, SQLCode, Param, Comment) values(" & strOwner & ", " & strNodeText & ", 0, " & strSQLCode & ", " & strParam & ", " & strComment & ")"
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("./App_Data/Reports.mdb") & ";User Id=admin;Password=;")
com = New OleDbCommand(strSql, con)
com.Connection.Open()
com.ExecuteNonQuery()
com.Connection.Close()
disp_myrep()
Catch
msg = "<h1 class=""ErrMsg"">Error Encountered: </h1>" & "<br/><b><font color=""red"">Error Source: " & Err.Source & "<br/>Error Description: " & Err.Description & "</font></b>"
msg = msg & strSql
End Try
If con.State = ConnectionState.Open Then
con.Close()
End If
End Sub
Similar code for my another program is working but I'm unable to trace where the problem is.
I also tried another approach as in the below code, but this also gave the same error.
Code: vb
Protected Sub AddNewSQLCode(ByVal sender As Object, ByVal e As EventArgs)
Dim strOwner As String = Chr(34) & Session("loginname") & Chr(34)
Dim strNodeText As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewNode"), TextBox).Text & Chr(34)
Dim strSQLCode As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewSQL"), TextBox).Text & Chr(34)
Dim strParam As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewParam"), TextBox).Text & Chr(34)
Dim strComment As String = Chr(34) & DirectCast(grdReport.FooterRow.FindControl("txtNewComm"), TextBox).Text & Chr(34)
Try
strSql = "insert into myreports(Owner, Node, IsTree, SQLCode, Param, Comment) values(@owner, @node, 0, @sql, @param, @comm)"
con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("./App_Data/Reports.mdb") & ";User Id=admin;Password=;")
com = New OleDbCommand(strSql, con)
com.CommandText = strSql
com.Parameters.AddWithValue("@owner", strOwner)
com.Parameters.AddWithValue("@node", strNodeText)
com.Parameters.AddWithValue("@sql", strSQLCode)
com.Parameters.AddWithValue("@param", strParam)
com.Parameters.AddWithValue("@comm", strComment)
com.Connection.Open()
com.ExecuteNonQuery()
com.Connection.Close()
disp_myrep()
Catch
msg = "<h1 class=""ErrMsg"">Error Encountered: </h1>" & "<br/><b><font color=""red"">Error Source: " & Err.Source & "<br/>Error Description: " & Err.Description & "</font></b>"
msg = msg & strSql
End Try
If con.State = ConnectionState.Open Then
con.Close()
End If
End Sub
