Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Code is executing without any error, but database is not updating.

What I have tried:

same query i run in sql and its work successfully. I tried by assigning the datagrid value directly and also tried by storing the value in a variable and then put it in query.
Here is my code.

VB
'generating transaction ID
 Dim dateAndTime As Date = Now
 Dim tra_id As Long = Format(dateAndTime, "ddHHmm")

 'inserting into database
 db_connect.Open()
 Dim indate As Date = System.DateTime.Today
 Dim R_index As Integer
 For R_index = 0 To dataGrid_incoming.Rows.Count - 2


     Dim tempLot As String = dataGrid_incoming.Rows(R_index).Cells(1).Value.ToString
     Dim temptot As String = dataGrid_incoming.Rows(R_index).Cells(2).Value.ToString
     Dim tempqty As Integer = dataGrid_incoming.Rows(R_index).Cells(3).Value
     Dim tempdet As String = dataGrid_incoming.Rows(R_index).Cells(4).Value.ToString

     Dim cmd As SqlClient.SqlCommand = db_connect.CreateCommand()
     cmd.CommandText = "Insert into item (tra_id, lot_no, totul ,qty , tra_status ) Values(" & tra_id & ", '" & tempLot & "', '" & temptot & "'," & tempqty & ", 'In_coming')"
     ' cmd.CommandText = "Insert into item (tra_id, lot_no, totul ,qty , tra_status ) Values(" & tra_id & ", '" & dataGrid_incoming.Rows(R_index).Cells(1).Value.ToString & "', '" & dataGrid_incoming.Rows(R_index).Cells(2).Value.ToString & "'," & dataGrid_incoming.Rows(R_index).Cells(3).Value & ", 'In_coming')"

     Dim cmd2 As SqlClient.SqlCommand = db_connect.CreateCommand()
     cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & tempdet & "')"
     'cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & dataGrid_incoming.Rows(R_index).Cells(4).Value.ToString & "')"

 Next

 db_connect.Close()
 dataGrid_incoming.Rows.Clear()
Posted
Updated 11-May-18 2:17am
v2

VB
cmd2.CommandText = "Insert into item_in (cus_id , tra_id , in_date ,in_detail) Values(" & cs_id & ", " & tra_id & ", " & indate & ", '" & tempdet & "')"

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
CHill60 11-May-18 8:18am    
5'd. It would actually partially solve the OP's problem as well - see my solution
Patrice T 11-May-18 8:29am    
thank you.
In fact I didn't even tried to guess what is wrong because only the resulting string or the error message on server can really help.
If you use a parameterized query then the sql will probably work - see indate? It's a date but there are no single-quotes around it in the final sql statement.

However, the sql will only work if you actually run it. You are missing the calls to ExecuteNonQuery Method[^] - one each for cmd and cmd2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900