|
I have a report project where a timer is polling the DB for a user abort. Once the abort is discovered I want to call a procedure on the master thread interrupting it so that it can abort the report. This way I don't have to get checking for an abort in the master thread.
I tried using delegates and the invoke method hoping that it would work like the form.invoke which calls the procedure in the form's thread. But it didn't work. Anyone know how to do this without using forms?
Here is a simple example:
Delegate Sub AbortDelegate()
Public Class ReportProcesser
Private _reportThreadID As Integer
Friend Abort As New AbortDelegate(AddressOf AbortReport)
Public WithEvents CheckForAbortTimer As New Timers.Timer(500)
Public ReadOnly Property ManagedThreadID() As Integer
Get
Return _reportThreadID
End Get
End Property
Public Sub New()
_reportThreadID = Threading.Thread.CurrentThread.ManagedThreadId
End Sub
Public Sub AbortReport()
MsgBox("Report Thread ID: " & Me.ManagedThreadID & vbCrLf & _
"Abort Report On Thread ID: " & Threading.Thread.CurrentThread.ManagedThreadId & vbCrLf)
End Sub
Private Sub CheckForAbortTimer_Elapsed() Handles CheckForAbortTimer.Elapsed
Static alreadyProcessing As Boolean = False
If Not alreadyProcessing Then
alreadyProcessing = True
Me.Abort.Invoke()
alreadyProcessing = False
End If
End Sub
End Class
Module Main
Sub main()
Dim report As New ReportProcesser
report.CheckForAbortTimer.Start()
Dim startTime As Date = Now
Do Until DateDiff(DateInterval.Minute, startTime, Now) >= 5
'end in 5 min or when stopped from Visual studio
Loop
End Sub
End Module
Thanks.
|
|
|
|
|
Hi all.
I want to know that how can I identify if a file is an application or a setup by vb.net programming with the extension ".exe".
Suggest me some ways.
Thanks.
Gagan
|
|
|
|
|
There's no way to tell the difference. An .exe is an .exe is an .exe. There's no way to tell what that .exe does.
|
|
|
|
|
Okay. Thanks for your suggestion.
Gagan
|
|
|
|
|
setup exe's do not live on their own, at least, not .Net ones. There are ways to package them as a single exe, however the setup.exe generated by .net deployment projects is just a stub calling the msi in the same folder. Most standalone exe installers are some form of self extracting archive, so there should be headers in that type of file that can be read. if you unpack those files you will find msi files, and maybe cab files inside.
|
|
|
|
|
Good day,
I have tried various methods to update an SQL database, but every time the database is updated but not the dataset.
I have done it with addrow and accept changes and with execute no query, but the dataset does not register the changes made. I have to MAIN.data.DS.Tables.Remove("Orders") and then add the datatable back to the dataset with the data adapter:
Example 1:
Function Add_Orders_Row() As Boolean
Try
Dim RowISAdded As Boolean = False
If txtQuantity.Text <> "" And txtProductionNo.Text <> "" _
And txtWeek.Text <> "" And txtPart.Text <> "" _
And cboProductCode.Text <> "" And txtPart.Text <> "" _
And txtWorksOrderNo.Text <> "" Then
Dim dt As DataTable = MAIN.data.DS.Tables("Orders")
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = cboOrderStatus.Text
dr.Item("SamsungRef") = "NA"
dr.Item("ProductCode") = cboProductCode.Text
dr.Item("ProductSerialNo") = txtProductSerialNo.Text
dr.Item("ProductionNo") = txtProductionNo.Text
dr.Item("WeekNo") = txtWeek.Text
dr.Item("PartNo") = CType(txtPart.Text, Integer)
dr.Item("OrderDate") = dtpOrderDate.Text
dr.Item("QuantityOrder") = CType(txtQuantity.Text, Integer)
dr.Item("QuantityScanned") = 0
dr.Item("WorksOrderNo") = txtWorksOrderNo.Text
If txtFirstSerial.Text = "" Then
dr.Item("FirstSerialNo") = "<SCAN>"
dr.Item("LastSerialNo") = "<CALC>"
Else
dr.Item("FirstSerialNo") = txtFirstSerial.Text
dr.Item("LastSerialNo") = txtLastSerial.Text
End If
dr.Item("timenow") = Date.Now
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Orders"
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(MAIN.data.DS, "Orders")
MAIN.data.DS.Tables.Remove("Orders")
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
Dim findrow As DataRow() = dt.Select("ProductionNo= '" & txtProductionNo.Text & "'")
If findrow.Length = 1 Then
RowISAdded = True
Else
RowISAdded = False
End If
Else
MessageBox.Show("Cannot add record, as some fields are blank. Please add all relevant data.")
RowISAdded = False
End If
Return RowISAdded
Catch ex As Exception
MessageBox.Show("Add_Orders_Row, " & ex.Message)
Return False
End Try
End Function
Example 2:
Function UNIT_Add_New() As Boolean
Try
Dim UnitIsAdded As Boolean = False
Dim strSQL As String = "INSERT INTO UNITS " & _
"(ScanCount,OrderID,SerialNo,ScanStatus,Description,ScanDate, " & _
"SamsungRef, ProductCode,ProductSerialNo,ProductionNo,WeekNo," & _
"PartNo,OrderDate,QuantityOrder,FirstSerialNo,LastSerialNo,WorksOrderNo)" & _
"VALUES" & _
"(@ScanCount,@OrderID,@SerialNo,@ScanStatus,@Description,@ScanDate,@SamsungRef, " & _
"@Productcode,@ProductSerialNo,@ProductionNo,@WeekNo,@PartNo,@OrderDate,@QuantityOrder, " & _
"@FirstSerialNo,@LastSerialNo,@WorksOrderNo); "
Dim da As New SqlClient.SqlDataAdapter
da.SelectCommand = New SqlClient.SqlCommand("SELECT * FROM UNTIS", MAIN.data.Conn)
Dim cmdAddNew As New SqlClient.SqlCommand(strSQL, MAIN.data.Conn)
cmdAddNew.Parameters.Add("@OrderID", SqlDbType.Int).Value = Me.drFoundOrder.Item("OrderID")
cmdAddNew.Parameters.Add("@ScanCount", SqlDbType.Int).Value = Units_Scanned()
cmdAddNew.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50).Value = txtSerialNo.Text
cmdAddNew.Parameters.Add("@ScanStatus", SqlDbType.VarChar, 50).Value = txtScanStatus.Text
cmdAddNew.Parameters.Add("@Description", SqlDbType.VarChar, 255).Value = txtDescription.Text
cmdAddNew.Parameters.Add("@ScanDate", SqlDbType.DateTime).Value = Date.Now
cmdAddNew.Parameters.Add("@SamsungRef", SqlDbType.VarChar, 50).Value = "NA"
cmdAddNew.Parameters.Add("@ProductCode", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("ProductCode")
cmdAddNew.Parameters.Add("@ProductSerialNo", SqlDbType.VarChar, 50).Value = Me.drOrder.Item("ProductSerialNo")
cmdAddNew.Parameters.Add("@ProductionNo", SqlDbType.VarChar, 50).Value = Me.drOrder.Item("ProductionNo")
cmdAddNew.Parameters.Add("@WeekNo", SqlDbType.VarChar, 3).Value = Me.drOrder.Item("WeekNo")
cmdAddNew.Parameters.Add("@PartNo", SqlDbType.SmallInt).Value = CType(Me.drOrder.Item("PartNo"), Integer)
cmdAddNew.Parameters.Add("@OrderDate", SqlDbType.DateTime).Value = Me.drFoundOrder.Item("OrderDate")
cmdAddNew.Parameters.Add("@QuantityOrder", SqlDbType.SmallInt).Value = CType(Me.drFoundOrder.Item("QuantityOrder"), Integer)
cmdAddNew.Parameters.Add("@FirstSerialNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("FirstSerialNo")
cmdAddNew.Parameters.Add("@LastSerialNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("LastSerialNo")
cmdAddNew.Parameters.Add("@WorksOrderNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("WorksOrderNo")
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim itest As Integer = cmdAddNew.ExecuteNonQuery()
'da.InsertCommand = cmdAddNew
'da.Update(MAIN.data.DS, "UNITS")
MAIN.data.DS.Tables("UNITS").AcceptChanges()
If MAIN.data.Conn.State = ConnectionState.Open Then MAIN.data.Conn.Close()
If itest = 1 Then
UnitIsAdded = True
Else
UnitIsAdded = False
End If
Return UnitIsAdded
Catch ex As Exception
MessageBox.Show("UNIT_Add_New, " & ex.Message)
Return False
End Try
End Function
Both do the same that SQL database is updated, but the dataset does not know what happened. So you cannot count the rows or get data from the new row in the dataset.
|
|
|
|
|
Read up on what AcceptChanges does first.
By calling that, you're telling the dataset that all changes that need to be written out the database HAVE been written out to the database, even if you didn't call Update first.
|
|
|
|
|
Hi,
Thanks for your reply.
I only added the accept changes later when I noticed that the ADO.net dataset-datatable is not updating. The SQL database contains the new data that was added, but the change is not reflected back to the Dataset-datatable.
This is what I am doing now to update the data set
Function Load_Table(ByVal SQLString As String, ByVal TableName As String) As Boolean
Dim SQLstr As String
Dim tableISadded As Boolean = False
Try
'open connection
If SQLcon.State = ConnectionState.Closed Then
SQLcon.Open()
End If
SQLstr = SQLString
Dim cmd As SqlCommand = New SqlCommand(SQLstr, SQLcon)
Dim da As New SqlDataAdapter(SQLstr, SQLcon)
da.Fill(pDS, TableName)
da.FillSchema(pDS, SchemaType.Mapped)
For Each dt As DataTable In pDS.Tables
If dt.TableName = TableName Then
tableISadded = True
Else
tableISadded = False
End If
Next
Return tableISadded
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
Finally
'close connection
SQLcon.Close()
End Try
End Function
'Remove the orders table
MAIN.data.DS.Tables.Remove("Orders")
''Reload datatable to dataset.
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
|
|
|
|
|
You're not saying what pDS is. You're also not showing what happens to pDS before Load_Table gets called.
|
|
|
|
|
Hi,
Hope this helps.
Datalayer class
Dim pDS As New DataSet
Public Property DS() As DataSet
Get
Return pDS
End Get
Set(ByVal value As DataSet)
pDS = value
End Set
End Property
Sub MakeConnect()
SQLcon.ConnectionString = "Data Source=192.168.10.24\BDMS;" & _
"Initial Catalog=FLATPAN;" & _
"User ID=flatpan;" & _
"Password=*******"
Try
SQLcon.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Function Load_Table(ByVal SQLString As String, ByVal TableName As String) As Boolean
Dim SQLstr As String
Dim tableISadded As Boolean = False
Try
'open connection
If SQLcon.State = ConnectionState.Closed Then
SQLcon.Open()
End If
SQLstr = SQLString
Dim cmd As SqlCommand = New SqlCommand(SQLstr, SQLcon)
Dim da As New SqlDataAdapter(SQLstr, SQLcon)
da.Fill(pDS, TableName)
da.FillSchema(pDS, SchemaType.Mapped)
For Each dt As DataTable In pDS.Tables
If dt.TableName = TableName Then
tableISadded = True
Else
tableISadded = False
End If
Next
Return tableISadded
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
Finally
'close connection
SQLcon.Close()
End Try
On the orders form call the Load_Table function to add the table to the dataset. This works.
Private Sub Orders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Load the orders table and lu products table
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders") 'THIS IS THE DA FILL FOR ORDERS
MAIN.data.Load_Table("SELECT * FROM LU_Products", "LU_Products")
MAIN.data.Load_Table("SELECT * FROM Status_Orders", "Status_Orders")
Calculate_Total_Rows(0, "LOAD") 'SOME FORM ACTIONS
Fields_Load(rowCounter) 'NOT PART OF UPDATE ONLY TO VIEW DATA
Fields_State("LOCK") 'LOCK FIELDS SO USER CANT CHANGE
End Sub
Here add a new row to the ORDERS table. This works, but cannot see the changes to the database until I remove the datatable from the dataset and then fill it back again. I think this would be very noisy as we scan barcodes into a table that will become very large over time.
Function Add_Orders_Row() As Boolean
Try
Dim RowISAdded As Boolean = False
If txtQuantity.Text <> "" And txtProductionNo.Text <> "" _
And txtWeek.Text <> "" And txtPart.Text <> "" _
And cboProductCode.Text <> "" And txtPart.Text <> "" _
And txtWorksOrderNo.Text <> "" Then
Dim dt As DataTable = MAIN.data.DS.Tables("Orders")
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = cboOrderStatus.Text
dr.Item("SamsungRef") = "NA"
dr.Item("ProductCode") = cboProductCode.Text
dr.Item("ProductSerialNo") = txtProductSerialNo.Text
dr.Item("ProductionNo") = txtProductionNo.Text
dr.Item("WeekNo") = txtWeek.Text
dr.Item("PartNo") = CType(txtPart.Text, Integer)
dr.Item("OrderDate") = dtpOrderDate.Text
dr.Item("QuantityOrder") = CType(txtQuantity.Text, Integer)
dr.Item("QuantityScanned") = 0
dr.Item("WorksOrderNo") = txtWorksOrderNo.Text
If txtFirstSerial.Text = "" Then
dr.Item("FirstSerialNo") = "<SCAN>"
dr.Item("LastSerialNo") = "<CALC>"
Else
dr.Item("FirstSerialNo") = txtFirstSerial.Text
dr.Item("LastSerialNo") = txtLastSerial.Text
End If
dr.Item("timenow") = Date.Now
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Orders"
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(MAIN.data.DS, "Orders")
'REMOVE THE DATABLE
MAIN.data.DS.Tables.Remove("Orders")
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
'MAKE SURE UPDATE MADE
Dim findrow As DataRow() = dt.Select("ProductionNo= '" & txtProductionNo.Text & "'")
If findrow.Length = 1 Then
RowISAdded = True
Else
RowISAdded = False
End If
Else
RowISAdded = False
End If
Return RowISAdded
Catch ex As Exception
MessageBox.Show("Add_Orders_Row, " & ex.Message)
Return False
End Try
End Function
|
|
|
|
|
This is hard to follow. Why are you apparently keeping the entire database, or at least entire tables, in memory? This is a design that will offer a performance gain for a small amount of time. The machine will eventually start swapping your app out to disk, slowing performance down dramatically.
|
|
|
|
|
Hmm...ok, my first reply may not be your problem. I may have misunderstood your problem. Let me see if I understand now.
In the first example, I still believe it's because you haven't set up the TableMapping . Though, I have to say, your code is a little hard to read and I believe a little non-standard.
As to the second example, TableMapping isn't your issue because you're running ExecuteNonQuery . That section of code should update the database, but it won't update your DataTable in memory. You have to understand the basics of ADO.Net. When the DataTable is created, it basically makes a copy of the database in memory. It is not linked at all to the underlying Database. That's why you have to run the Update. If the TableMapping is set up properly, then, when the Update is called, it tells the DataAdapter to find the matching Table in the matching DataSet and update the cells based on their RowState property. If the RowState indicated Insert, then the SQL Insert is called. If the RowState says Modify, then it runs the SQL Update. If it says Delete, then it runs the SQL Delete.
But, there is no static link between your DataTable and the DataSet. When you run the SQL Insert, you also will have to refresh your DataTable, or insert the data into the DataTable as well.
|
|
|
|
|
If you're using ADO.Net, I believe you have to set up the TableMapping before the Update will work.
See: Table Mapping in ADO.Net[^]
|
|
|
|
|
As far as the second example...I don't think this is affecting it, because it doesn't look like you use it again, but you misspelled UNITS in the Select Statement.
Dim da As New SqlClient.SqlDataAdapter
da.SelectCommand = New SqlClient.SqlCommand("SELECT * FROM UNTIS", MAIN.data.Conn)
|
|
|
|
|
Hi,
I am a little confused now. the code that I am using is based on several references and websites. I thought that was the idea of ADO.net. Work with data offline. What is the corrct way of doing this.
I have tried to simplify my code below.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Example
Dim SQLcon As New SqlConnection
Dim pDS As New DataSet
Sub MakeConnect()
SQLcon.ConnectionString = "Data Source=192.168.10.24\BDMS;" & _
"Initial Catalog=FLATPAN;" & _
"User ID=flatpan;" & _
"Password=*********"
Try
SQLcon.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub Load_Table()
Dim SQLstr As String = "SELECT * FROM Orders"
Try
'open connection
If SQLcon.State = ConnectionState.Closed Then
SQLcon.Open()
End If
Dim cmd As SqlCommand = New SqlCommand(SQLstr, SQLcon)
Dim da As New SqlDataAdapter(SQLstr, SQLcon)
da.Fill(pDS, "ORDERS")
'Added this to see if it helped
da.FillSchema(pDS, SchemaType.Mapped)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close connection
SQLcon.Close()
End Try
End Sub
Sub Add_New_Record()
Try
Dim dt As DataTable = pDS.Tables("Orders")
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = "A"
dr.Item("SamsungRef") = "B"
dr.Item("ProductCode") = "C"
dr.Item("ProductSerialNo") = "D"
dr.Item("ProductionNo") = "E"
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Orders"
If SQLcon.State = ConnectionState.Closed Then SQLcon.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(pDS, "Orders")
'Had to add this to register change in dataset
pDS.Tables.Remove("Orders")
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
Catch ex As Exception
MessageBox.Show("Add_New_Record, " & ex.Message)
End Try
End Sub
End Class
|
|
|
|
|
Sorry this should just be
Load_Table
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
|
|
|
|
|
I think I found the problem. Not sure why this makes a difference?
da.FillSchema(pDS, SchemaType.Source, TableName)
|
|
|
|
|
You are correct...ADO.Net works with data offline. That's what I was saying. When you run an ExecuteNonQuery like in your second example, the DataSet that you filled previously will not reflect those changes because the DataSet is an offline version and the ExecuteNonQuery is updating the Online Version.
The FillSchema should do the trick...though I'm not as familiar with it...it looks like it's there to set up the TableMapping for you. That will work for the first example. Let me show you an example of code that I run to add a new row to a database called "1 Populations"
Dim openedConnectionString As Boolean = False
If Not connectionString.State = ConnectionState.Open Then
connectionString.Open()
openedConnectionString = True
End If
Dim insertionDS As New DataSet
Dim newDataAdapter As OleDbDataAdapter
Dim sql As String = "SELECT * FROM [" & ActiveProject.Identifier & " Populations] ORDER BY ID;"
newDataAdapter = New OleDb.OleDbDataAdapter(sql, connectionString)
newDataAdapter.TableMappings.Add("Table", "Populations")
With newDataAdapter.TableMappings(0).ColumnMappings
.Add("ID", "ID")
.Add("Name", "Name")
.Add("Char ID", "Char ID")
.Add("Stratum", "Stratum")
.Add("PopLevel", "PopLevel")
End With
Dim cb As New OleDbCommandBuilder(newDataAdapter)
cb.QuotePrefix = " ["
cb.QuoteSuffix = "] "
newDataAdapter.Fill(insertionDS)
Dim lastIndex As Integer = 0
With insertionDS.Tables("Populations")
If .Rows.Count <> 0 Then
lastIndex = .Rows(.Rows.Count - 1).Item("ID")
End If
Dim newRow As DataRow = .Rows.Add
newRow.BeginEdit()
newRow.Item("ID") = lastIndex + 1
newRow.Item("Name") = Population
newRow.Item("Char ID") = ""
newRow.Item("Stratum") = Stratum
newRow.Item("PopLevel") = Level
newRow.EndEdit()
End With
newDataAdapter.InsertCommand = cb.GetInsertCommand
newDataAdapter.Update(insertionDS)
newDataAdapter.Dispose()
If openedConnectionString Then
connectionString.Close()
End If
When I run this, it adds a new row to both the DataSet and the Database.
|
|
|
|
|
Hi All,
here just i want to know how to get the Internet connection Status in VB6 whether it is working/not working.
if yes, let me also know how it is?
Thanks in advance.
Jay
|
|
|
|
|
jayachandra.c wrote: here just i want to know how to get the Internet connection Status in VB6 whether it is working/not working.
The most used way would be using InternetGetConnectedState API. An example can be found here[^].
Alternatively, you could launch the Ping command and capture it's output. If you can Ping Google, you're online I are Troll
|
|
|
|
|
I would say that is not necessarily true. It depends on what you mean by 'working'. You could be connected but have DNS issues that would prevent a successful ping. I only say that because I did have DNS issues and ran into this often.
|
|
|
|
|
Dear All,
I have issue with the exhadecimal.
I want to convert it to text by VB.net and FoxPro 6.0.
But i do not know any thing about it.
Do you have any idea or solving of this problem ?
Thanks for your help...VB.Net
|
|
|
|
|
Golden Jing wrote: I have issue with the exhadecimal.
I want to convert it to text
The hexadecimal "&hFF" would have a tekstual representation that equals "FF".
Console.WriteLine(Convert.ToInt32("FF", 16)) The above example converts the value "FF" to a decimal value, which should be 255 for "FF".I are Troll
|
|
|
|
|
|
Dear Eddy,
Thanks for you help.
But now my problem now is that the hexadecimal is so long.
mean that it is sentent so i want to convert that to the text.
have any solving of that ?VB.Net
|
|
|
|
|