No data available after an Insert operation VB.NET - Windows Forms and Web Service - vb.net

VB.NET and ASMX Web Service. I am still having the same trouble of getting no data after an insert operation.
Here is my insert code:
Try
'create a MySqlCommand to represent the query
If sqlConn.State = ConnectionState.Closed Then
sqlConn = New MySqlConnection(conStr)
sqlConn.Open()
End If
Dim myCommand As MySqlCommand = New MySqlCommand(strQuery, sqlConn)
If myCommand.ExecuteNonQuery() <> 0 Then
Result = True
End If
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
Finally
sqlConn.Close()
sqlConn.Dispose()
End Try
The strQuery will be a simple insert statement.
The code for retrieving data as a dataset is as follows
Public Function ExecuteQuery(ByVal strQuery As String) As DataSet
Dim ds As DataSet = New DataSet 'create a DataSet to hold the results
Try
'create a MySqlCommand to represent the query
If sqlConn.State = ConnectionState.Closed Then
sqlConn = New MySqlConnection(conStr)
sqlConn.Open()
End If
Dim sqlcmd As MySqlCommand = sqlConn.CreateCommand()
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = strQuery
'create a MySqlDataAdapter object
Dim sqlDa As MySqlDataAdapter = New MySqlDataAdapter
sqlDa.SelectCommand = sqlcmd
Try
'fill the DataSet using the DataAdapter
sqlDa.Fill(ds, "Results")
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 1 " & ex.Message)
End Try
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
Finally
sqlConn.Close()
sqlConn.Dispose()
End Try
Return ds
End Function
I am Inserting data from one thread and retrieving data from another (not the same instance of connection is used).
This works perfect if I retrieve data after a 15 minutes gap.
But I want to get the result at once.
Please help.

I would change the method as below
Public Function ExecuteQuery(strQuery As String) As DataSet
Dim ds As New DataSet()
Try
Using con = New MySqlConnection(conStr) 'using statements...
Using cmd = New MySqlCommand(strQuery, con) 'using statements...
Using sqlDa = New MySqlDataAdapter(cmd) 'using statements...
sqlDa.Fill(ds, "Results")
End Using
End Using
End Using
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 1 " + ex.Message)
End Try
Return ds
End Function
Public Function ExecuteNonQuery(strQuery As String) As Boolean
Try
Using con = New MySqlConnection(conStr) 'using statements...
Using cmd = New MySqlCommand(strQuery, con) 'using statements...
con.Open()
If cmd.ExecuteNonQuery() > 0 Then ' I have change the condition
Return True
End If
End Using
End Using
Catch ex As Exception
Throw New ApplicationException("Error-luckie's server 2 " + ex.Message)
End Try
Return False
End Function

Related

No results to select sum to another table with OLEDB in VB.NET

Dear All Master,
I have tried but there is no result from sql select sum and only appears group from the column "PNM". Is there anything wrong with the sql I created?. is there any other solution?.
I don't know why it doesn't appear in the sum value in the "BLC" column in the TEMPTABL table.
Thanks
Private Sub fillDataGridView1()
Try
Dim query As String = "SELECT PNM,NOD,QTY,CIU,DPR FROM GSDTS WHERE QTY > 0"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
Me.DataGridView1.DataSource = source1
Me.DataGridView1.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Private Sub fillDataGridView2()
Try
Dim query As String = "select * FROM TEMPTABL"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source2.DataSource = dt
Me.DataGridView2.DataSource = source2
Me.DataGridView2.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Sub deltemptabl()
Try
Dim sql As String = "DROP TABLE TEMPTABL"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch myerror As Exception
MessageBox.Show("Error: " & myerror.Message)
End Try
End Sub
Sub temptablsum()
Try
'here is the line of code below sql select sum
Dim sql As String = "select PNM, sum((qty*ciu)*(1-dpr/100)) AS BLC INTO TEMPTABL from GSDTS group by PNM"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch myerror As Exception
MessageBox.Show("Error: " & myerror.Message)
End Try
End Sub
DPR is null, therefore it will return null when you try to compute it. You can account for that with isnull(), setting it to 1 (or whatever fits the scenario) whenever the column could be null
sum((qty*ciu)*(1-ISNULL(dpr,1)/100))
Edit:
After looking over it again, you may want to apply the isnull function to the entire sum to return 0 if null. It's difficult to give exacts as I dont know what each column means or specifics to the scenario
ISNULL(sum((qty*ciu)*(1-dpr/100)),0)
Edit/option 2:
Encapsulate that column in a case statement to handle the scenario if needed.
CASE WHEN DPR IS NOT NULL THEN sum((qty*ciu)*(1-dpr/100)) ELSE ReturnSomethingElse END AS DPR
Option 3:
Change the table column and front end to not accept null values if it's not viable for the scenario

if theres no record display in datagridview then messagebox no member found

im using datagridview to display employee details and when theres no record found there will be a messagebox that there were no record found here's my code.
Dim search As String = String.Empty
search &= "select * from record "
search &= "where identification=#identification;"
Using conn As New SqlConnection("server=KENJOY_FMCD;database=humanresource;user=ayala747;password=4525422;")
Using cmd As New SqlCommand()
With cmd
.Connection = conn
.CommandType = CommandType.Text
.CommandText = search
.Parameters.AddWithValue("#identification", vsearch.Text)
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
If ds.Tables.Count >= 0 Then
DataGridView1.DataSource = ds.Tables(0)
End If
If (ds Is Nothing) Then
MsgBox("No record found!")
End If
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
End Class

Update combobox after insert a value

I want to update the Combobox after insert a value that goes straight into the Combobox. Probably I need to make a query and I already got it. That is the code that I got at the moment
Private Sub UpdateComboBox()
SQLCon = New SqlConnection
SQLCon.ConnectionString = "...."
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
SQLDataReader = SqlCmd.ExecuteReader
ComboBox1.DataSource = ComboBox1.Items.Add(Query)
SQLCon.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
End Sub
and I want to add this method at the moment after insert something.
This code is in the form
SQLCon = New SqlConnection
SQLCon.ConnectionString = "......"
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
SQLDataReader = SqlCmd.ExecuteReader
While SQLDataReader.Read
Dim fileType = SQLDataReader.GetString(0)
DataGridView1.DataSource = ComboBox1.Items.Add(fileType)
End While
SQLCon.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
Try this :
Private Sub UpdateComboBox()
SQLCon = New SqlConnection
SQLCon.ConnectionString = "....."
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
Dim adapter As New SqlDataAdapter(SqlCmd)
Dim table As New DataTable
adapter.Fill(table)
ComboBox1.DataSource = table
ComboBox1.DisplayMember = "Filetype"
SQLCon.Close()
Return table
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
End Sub

How to refresh GridView's data after update or delete operation in case of , GridControl.datasource = Datatable in Devexpress

I have a devexpress gridview that is related to a table in a sql server database. I am trying to refresh the gridview right after performing a delete but nothing I've tried so far has worked. (I've verified that the delete operation has worked in the table). I've tried 3 ways of updating the gridview but nothing has worked:
GridControl1.Refresh()
GridView1.RefreshData()
GridControl1.RefreshDataSource()
GridView1.RefreshEditor(True)
Here is the complete code of the whole operation:
Using cnn As New SqlConnection(FrmGeneralConfig.GetInstance.getConnection())
Try
cnn.Open()
Dim daDelete As New SqlDataAdapter
Dim command As SqlCommand = New SqlCommand("DELETE FROM Client_Excepte_Charge_Min WHERE ClientNo = #ClientNo", cnn)
command.Parameters.AddWithValue("#ClientNo", clientNo)
daDelete.DeleteCommand = command
daDelete.DeleteCommand.ExecuteNonQuery()
cnn.Close()
Catch ex As Exception
MessageBox.Show("Erreur: " & ex.Message)
Finally
GridControl1.Refresh()
GridView1.RefreshData()
GridView1.RefreshEditor(True)
End Try
End Using
You need to update your DataTable after deleting the rows in server.
Here is example:
Using cnn As New SqlConnection(FrmGeneralConfig.GetInstance.getConnection())
Try
cnn.Open()
Dim command As SqlCommand = New SqlCommand("DELETE FROM Client_Excepte_Charge_Min WHERE ClientNo = #ClientNo", cnn)
command.Parameters.AddWithValue("#ClientNo", clientNo)
command.ExecuteNonQuery()
dtExceptions.Clear()
Using adapter As New SqlDataAdapter()
Dim selectCommand = New SqlCommand("SELECT * FROM Client_Excepte_Charge_Min", cnn)
adapter.SelectCommand = selectCommand
adapter.Fill(dtExceptions)
End Using
cnn.Close()
Catch ex As Exception
MessageBox.Show("Erreur: " & ex.Message)
End Try
End Using

There is already an open DataReader associated with this Connection which must be closed first VB.NET

I get this error message
"There is already an open DataReader associated with this Connection
which must be closed first"
Please help me
My code is:
Public Sub update_qty(ByVal qry1 As String)
Dim dr As MySqlDataReader 'SQLiteDataReader
Dim comm As MySqlCommand 'SQLiteCommand
Try
comm = New MySqlCommand(qry1, conn)
dr = comm.ExecuteReader()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Do While dr.Read()
exe_query("call cargosys.paymentsAdd('" & var1 & "', " & dr("inNo") & ")")
Loop
dr.Close()
End Sub
Public Sub exe_query(ByVal qry As String) As String
Dim cmd As MySqlCommand
Try
cmd = New MySqlCommand(qry, conn)
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show(ex.ToString)
End Try
End Sub
Your problem is that your code open a DataReader and then execute the SqlCommand when the DataReader read
Try to change this line:
dr = comm.ExecuteReader()
to:
dr = comm.ExecuteReader(CommandBehavior.CloseConnection)
More: DataReader CommandBehavior
Or change your connection string to enable MARS (Multiple Active Result Sets).
This setting will allow for the retrieval of multiple forward-only, read-only result sets on the same connection.
For example :
connectionString=
"Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|Northwind.MDF;
Integrated Security=True;
User Instance=True;
MultipleActiveResultSets=True"
More: MARS
EDIT
Since MARS keyword is not supported, try to change your code to this:
Public Sub update_qty(ByVal qry1 As String)
Dim dr As MySqlDataReader 'SQLiteDataReader
Dim comm As MySqlCommand 'SQLiteCommand
Try
comm = New MySqlCommand(qry1, conn)
dr = comm.ExecuteReader()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim myList As New List(Of String)
Do While dr.Read()
myList.Add("call cargosys.paymentsAdd('" & var1 & "', " & dr("inNo") & ")")
Loop
dr.Close()
End Sub
Public Sub exe_query(myList As List(Of String))
Dim cmd As MySqlCommand
For Each query As String In myList
Try
cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show(ex.ToString)
End Try
Next
End Sub
Instead to doing DataReader.Read->SqlCommand.ExecuteNonQuery simultaneously, this code will be read all the data first and then run SqlCommand.ExecuteNonQuery.