I am reading data from SQL into my vb.net program and am getting incorrect data when I run the application. As such I have simplified my query to diagnose and when I run the code on my SQL management studio I get 2288 results but when I run the same code in my application I am only getting 1872 results.
SQL code:
SELECT * FROM invoiceItem
left join invoice on invoice.invoiceno = invoiceitem.invoiceno
where invoice.customer = 'aaapal'
VB.NET Code
Dim SQL As String
SQL = "SELECT * FROM invoiceItem " & _
"left join invoice on invoice.invoiceno = invoiceitem.invoiceno " & _
"WHERE invoice.customer = 'aaapal'"
Dim DT As New DataTable
Try
Dim con As New SqlConnection(My.Settings.TSMT_DBConnectionString)
Dim da As SqlDataAdapter = New SqlDataAdapter(SQL, con)
da.Fill(DT)
MessageBox.Show(DT.Rows.Count)
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error Loading top 10 Products", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return DT
Any suggestions as to what could be causing this issue?
Related
I'm using datetimepicker to choose the date to show the data from the microsoft sql server but the cells are empty.
I use LIKE operator to show the purchased item within the day.
This is the code:
Public Sub see4()
'MYPOPUL4TE D4T4GRIDVIEW3
Dim query As String = "SELECT * FROM projectsd.dbo.cart WHERE dates LIKE '" & DateTimePicker1.Text & "'"
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As New SqlDataAdapter
da.SelectCommand = cmd
Using dt As New DataTable()
da.Fill(dt)
DataGridView3.DataSource = dt
End Using
End Using
End Using
End Sub
I have developed an application which runs with multiple instance simultaneously. In the application it connects to a SQL Server and fetches some data, while working on that data, it updates the row as picked so that no other instance of the application picks up the same record for research. While updating the row for the fetched record, sometime it gets deadlock issue and not updating the record as already picked.
Dim MyConnection As SqlConnection
Try
MyConnection = New SqlConnection(connString)
MyConnection.Open()
Dim tableName As String = myTableName
Dim sqlQuery As String = "Select Top 1 * from " + tableName + " where "<some condition>
Dim MyCommand As SqlDataAdapter = New SqlDataAdapter(sqlQuery, MyConnection)
Dim DS as DataSet = New DataSet
MyCommand.Fill(DS, tableName)
If DS.Tables(0).Rows.Count >= 1 Then
sqlQuery = "UPDATE " + tableName + " SET Fld = #fld where Cond1= '" + DS.Tables(0).Rows(0).Item("Cond1").ToString + "'"
Dim cmd As New Data.SqlClient.SqlCommand(sqlQuery)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("#fld", Data.SqlDbType.VarChar).Value = "Picked"
Try
cmd.Connection = MyConnection
cmd.ExecuteNonQuery() <---- Dead lock occurs here
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
MyConnection.Close()
End Try
While executing the update statement, deadlock occurs and it goes to catch block and then exits the sub without updating the required field in the table. I want it to try to update until the dead lock is removed.
Creating Stored Procedure is not possible as I do not have the admin rights to this DB.
How can I overcome the problem?
i'm geek in vb.net, I already make vb.net from application with ms access data base. now i need to use this application for multi user and i want to use SQL server, i use this code to show data from access data base to listview :
con.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\noorapp.accdb;"
con.Open()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("select * from " & Year(Now) & " where cmonth='" & m & "' order by cdate DESC", con)
da.Fill(dt)
Dim myrow As DataRow
For Each myrow In dt.Rows
ListView1.Items.Add(myrow.Item(0)).ToString()
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(3))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(4))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(5))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(6))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(7))
Next
any solution to how make this work for sql server also?
thanks.
change your connection string see here. Also change your OleDbConnection object to SQLConnection and your OleDbDataAdapter to SQLDataAdapter
Your database must already exist in the SQL Server for this to work
Try
Dim path As String = String.Concat((Server.MapPath("~/temp/" + CCAM_Browe.FileName)))
CCAM_Browe.PostedFile.SaveAs(path)
Using OleDBcon As New OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended properties=Excel 12.0;")
Using cmd As New OleDbCommand("Select * from [Sheet1$]", OleDBcon)
Using objAdapterr1 As New OleDbDataAdapter(cmd)
OleDBcon.Open()
Dim dr As DbDataReader = cmd.ExecuteReader()
Dim con_str As String = "Data Source=GFCKUL-A0027716\;Initial Catalog=Inbound_VSM;Integrated Security=True"
Using bulkInsert As SqlBulkCopy = New SqlBulkCopy(con_str)
bulkInsert.DestinationTableName = "test"
bulkInsert.WriteToServer(dr)
End Using
OleDBcon.Close()
lblMsg.Text = "Upload Successful"
File.Delete(Convert.ToString(CCAM_Browe.PostedFile.FileName))
End Using
End Using
End Using
Catch ex As Exception
lblMsg.Text = "Upload Unsuccessful, Please contact Administrator to assist"
End Try
The error occur at line OleDBcon.open(). I'm fed up with this problem , i currently installed Microsoft Excel 2013 32-bit .
This code work fine with if it's not Report Excel file.
Please help me! Thanks!
I am getting a conversion error every time I try to submit a date value to sql server. The column in sql server is a datetime and in vb I'm using Date.today to pass to my parameterized query. I keep getting a sql exception
Conversion failed when converting datetime from character string.
Here's the code
Public Sub ResetOrder(ByVal connectionString As String)
Dim strSQL As String
Dim cn As New SqlConnection(connectionString)
cn.Open()
strSQL = "DELETE Tasks WHERE ProjID = #ProjectID"
Dim cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.AddWithValue("ProjectID", 5)
cmd.ExecuteNonQuery()
strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
" (#ProjID, #TaskName, #DueDate)"
Dim cmd2 As New SqlCommand(strSQL, cn)
cmd2.CommandText = strSQL
cmd2.Parameters.AddWithValue("ProjID", 5)
cmd2.Parameters.AddWithValue("DueDate", Date.Today)
cmd2.Parameters.AddWithValue("TaskName", "bob")
cmd2.ExecuteNonQuery()
cn.Close()
DataGridView1.DataSource = ds.Projects
DataGridView2.DataSource = ds.Tasks
End Sub
Any thoughts would be greatly appreciated.
Try
DateTime.Now()
Or
DateTime.Today;
Instead of
Date.Today
Could be an issue with different regional settings between your application and SQL server.
Although it doesn't resolve the underlying issue, a straight-forward way to avoid this is by explicitly formatting the date you pass to SQL
cmd2.Parameters.AddWithValue("DueDate", DateTime.Today.ToString("yyyyMMdd"));
EDIT:
I read your code more carefully: you've got your parameters and values in different orders. Try:
strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _
" (#ProjID, #DueDate, #TaskName)"
Parameters were in the wrong order. Doh!
#TaskName and #DueDate were swapped. I believe all of the above suggestion are correct.
strSQL = "INSERT INTO Tasks (ProjID, DueDate, TaskName) VALUES " & _ " (#ProjID, #TaskName, #DueDate)"