Dear sirs,
we are facing new issue while inserting data via VB.net but same query if i execute via SQL server the data inserting
so please help to overcome this issue
VB code
Dim kSQL As String = Query6.Replace("@", Requestid)
Dim cmd1 As New SqlCommand(kSQL, sqlSoftcodeCn)
cmd1.CommandTimeout = 1000000
cmd1.ExecuteNonQuery()
cmd1.Dispose()
SQL: (query6 this will pass)
INSERT INTO PdcChequeRemovalMaillogs(Request_id,WORKFLOW_LINE_ID,ROLE_ID,LEVEL_ID,STATUS_ID) select Top 1 header_id,line_id,role_id,level_id,status_id from FnGetPdcChequeRemovalApproverWithLink(7) Order by Level_id

Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 25, 2024, 6:40 AM EDT
Yesubabu, can you try this soulution:
Dim query As String = "INSERT INTO PdcChequeRemovalMaillogs(Request_id, WORKFLOW_LINE_ID, ROLE_ID, LEVEL_ID, STATUS_ID) SELECT TOP 1 header_id, line_id, role_id, level_id, status_id FROM FnGetPdcChequeRemovalApproverWithLink(@RequestId) ORDER BY Level_id" Try Using cmd As New SqlCommand(query, sqlSoftcodeCn) cmd.Parameters.AddWithValue("@RequestId", Requestid) cmd.CommandTimeout = 1000000 cmd.ExecuteNonQuery() End Using Catch ex As SqlException Console.WriteLine("SQL Error: " + ex.Message) Catch ex As Exception Console.WriteLine("General Error: " + ex.Message) End TryAmit MohantyRank #112 · 20K reputationPosted Jul 25, 2024, 6:02 AM EDT
Your Query6 does not contain the "@" symbol, so the replacement will have no effect.
Try this:
Dim Query6 As String = "INSERT INTO PdcChequeRemovalMaillogs(Request_id, WORKFLOW_LINE_ID, ROLE_ID, LEVEL_ID, STATUS_ID) " & "SELECT TOP 1 header_id, line_id, role_id, level_id, status_id " & "FROM FnGetPdcChequeRemovalApproverWithLink(@Requestid) " & "ORDER BY Level_id" Using sqlCommand As New SqlCommand(Query6, sqlSoftcodeCn) sqlCommand.CommandTimeout = 1000000 sqlCommand.Parameters.AddWithValue("@RequestID", Requestid) sqlCommand.ExecuteNonQuery() End Usingyesubabu kRank #2,091 · 687 reputationPosted Jul 25, 2024, 6:22 AM EDT
No sir, same issue query is executing but data is not saving table level
Amit MohantyRank #112 · 20K reputationPosted Jul 25, 2024, 6:35 AM EDT
You cannot use @ as a parameter placeholder in your SQL query you need to properly defined and handled in the query string. SQL parameters need specific names in both the SQL query and when adding them to the SqlCommand object.
Abhishek YadavRank #217 · 11.3K reputationPosted Jul 25, 2024, 6:43 AM EDT
Revised Code with Parameterized Query and Error Handling
Here is a revised version of your VB.NET code using parameterized queries and proper error handling:
' Ensure your connection string and SQL connection are correctly configured Dim connectionString As String = "Your Connection String Here" Using sqlSoftcodeCn As New SqlConnection(connectionString) Try sqlSoftcodeCn.Open() ' Define your query with parameters Dim kSQL As String = "INSERT INTO PdcChequeRemovalMaillogs(Request_id, WORKFLOW_LINE_ID, ROLE_ID, LEVEL_ID, STATUS_ID) " & _ "SELECT TOP 1 @RequestId, line_id, role_id, level_id, status_id " & _ "FROM FnGetPdcChequeRemovalApproverWithLink(7) ORDER BY Level_id" ' Create the command with parameters Using cmd1 As New SqlCommand(kSQL, sqlSoftcodeCn) cmd1.CommandTimeout = 1000000 ' Add parameters to avoid SQL injection cmd1.Parameters.AddWithValue("@RequestId", Requestid) ' Execute the command cmd1.ExecuteNonQuery() End Using Catch ex As Exception ' Log and handle the exception MessageBox.Show("An error occurred: " & ex.Message) Finally ' Ensure the connection is closed properly If sqlSoftcodeCn.State = ConnectionState.Open Then sqlSoftcodeCn.Close() End If End Try End UsingThings to Check:
PdcChequeRemovalMaillogstable match those being inserted from theFnGetPdcChequeRemovalApproverWithLink(7)function.Further Debugging:
If the issue persists, add more detailed logging to understand what part of the process fails. You can also capture the exact SQL query generated and execute it directly in SQL Server Management Studio (SSMS) to verify it works outside the application.
By using parameterized queries and proper error handling, you can avoid common issues and improve the reliability and security of your database operations.