Update item from Dataset adaptor - vb.net

I'm trying to update a row brought back from a dataset adaptor. I'm clearly doing something wrong but my googling/logic keeps failing me.
Heres what I have;
Public Class Popup
Inherits System.Web.UI.Page
Private dtJobs As Barry.joblistDataTable
Private taJobs As New BarryTableAdapters.joblistTableAdapter
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Type As String = Request.QueryString("t")
Select Case Type
Case "cancel"
CancelLocation(Request.QueryString("t"))
End Select
End Sub
Protected Sub CancelLocation(ByVal JobNo As String)
dtJobs = New Barry.joblistDataTable
dtJobs = taJobs.GetByJobNo(JobNo)
dtJobs.Item("Status") = "Cancelled"
taJobs.Update(dtJobs)
End Sub
End Class
The database field is called Status and I want to put the string of Cancelled in there

Related

ListChanged Event not firing?

I have a class with a bindinglist(of T) in it. The bindinglist is bound to a datagridview on my form. When items are added to the bindinglist, they show up in the datagridview however the scrollbar never changes to accommodate for the new data. I am starting to think this is because the Listchanged event isn't being fired (or properly captured by my form). I have my code set up like this:
Data Class:
Public Class data
Implements INotifyPropertyChanged
Public Sub new(byVal att1 as string, ByVal att2 as string)
Attribute1 = att1
Attribute2 = att2
End sub
Private mAttribute1 as string
Public Property Attribute1 as string
Get
return mAttribute1
End get
Set(ByVal value as string)
mAttribute1 = value
OnPropertyChanged("Attribute1")
End Set
End Property
Private mAttribute2 as string
Public Property Attribute2 as string
Get
return mAttribute2
End Get
Set(ByVal value as string)
mAttribute2 = value
OnPropertyChanged("Attribute2")
End Set
End Property
Public Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Public Sub ChangeDataFormat()
'change from one format to the other
End Sub
End Class
Data Generator Class:
Public Class dataGenerator()
private myThread as New System.Theading.Thread(address of StartDataGeneration)
Public Sub new()
mDataList = new bindingList(of Data)
mDataList.RaiseListChangedEvents = True
Private WithEvents mDataList as bindingList(Of Data)
Public readonly DataList as bindingList(of Data)
Get
Return mDataList
End Get
End property
Private Sub StartDataGeneration()
dim att1 as integer = 1
dim att2 as integer = 2
for i as Integer = 0 to 1000
mDataList.Insert(0,New Data(att1.ToString,att2.ToString)
att1 *= 2
att2 *=3
next
End Sub
Public Sub StartDataThread()
myThread.Start()
End Sub
Public Sub ChangeDataFormat()
for each d as data in mDataList
d.ChangeDataFormat()
next
End Sub
End Class
Form:
Public class Form1
Private myGenerators as new BindingList(of dataGenerator)
Private myDataGrids as new BindingList(of DataGridView)
Private Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Mybase.Load
dim NumberOfGenerators as integer = Convert.ToInt32(My.Settings.CraneCount)
for i as integer = 1 to NumberOfGenerators
Dim newGenerator As New DataGenerator()
Dim newTab as Ne tabPage(i.ToString)
Dim NewGrid as New DataGridView
newTab.Controls.Add(newGrid)
newGrid.DataSource = newGenerator.DataList
myGenerators.Add(newGrid)
next
End Sub
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
for each generator as dataGenerator in myGenerators
generator.StartDataThread()
next
End Sub
Private Sub ButtonChangeFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonChangeFormat.Click
for each generator as dataGenerator in myGenerators
generator.ChangeDataFormat()
next
End Sub
End Class
I know that there is a lot of code but I wanted to be clear. So when I click the start button the new items start appearing, however, once they get to the bottom of the grid the scroll bar doesn't appear. If I click the Change Format button the data changes format and updates in the grid properly. I was under the impression that the ListChanged event would automatically work with a bindinglist and datagridview. I tried calling update and refresh on myDataGridView and setting datagridview.datasource to nothing and then back to DataList.
Am I missing something?

Validating a checkListBox?

I have two forms (1 and 2). I have been battling with some code that would prevent the user from selecting an item in the checkedListBox that was not added into ListBox2 from the previous form (form1).
The code I have is kind of weird because even if the item was added to listbox2 from form1, it continues to display the msgBox. I need the msgBox to display only to those items that were not added to listbox2, form1.
Here is what I have:
Public Class Form1
Dim ActSubject As Boolean
Public Function ActivateSubject() As String
Return ActSubject
End Function
Private Sub ListBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.TextChanged
Dim x As New Items
x.AvailableItems = ListBox2.Items.ToString
For Each x In ListBox2.Items
If ListBox2.Items.Contains(x) Then
ActSubject = True
Else
ActSubject = False
End If
Next
End Sub
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If HaveActSubject = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class
Public Class Form1
Public Function ActivateSubject(itm as string) As String
Return ListBox2.Items.Contains(itm)
End Function
End Class
Public Class Form2
Dim HaveActSubject As Boolean = Form1.ActivateSubject
Private Sub CheckedListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedValueChanged
If Form1.ActivateSubject(CheckedListBox1.selectedValue) = False Then
MsgBox("Sorry! Subject should be activated six month before registration.")
End If
Return
End Sub
End Class

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub

VB.NET editing existing that with a form

I have a simple questions that puzzles me. I need a little bit of refreashment with VB as I have been away for a while. I have a form that adds new contacts. New contacts are added by pressing an appropriate button and they appear as an entry in the list on the form. I try now to add an edit button that will edit existing entries. User will select a given entry on the list and press edit button and will be presented with an appropriate form (AddContFrm). Right now it simply adds another entry with the same title. Logic is handled in a class called Contact.vb Here is my code.
Public Class Contact
Public Contact As String
Public Title As String
Public Fname As String
Public Surname As String
Public Address As String
Private myCont As String
Public Property Cont()
Get
Return myCont
End Get
Set(ByVal value)
myCont = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Cont
End Function
Sub NewContact()
FName = frmAddCont.txtFName.ToString
frmStart.lstContact.Items.Add(FName)
frmAddCont.Hide()
End Sub
Public Sub Display()
Dim C As New Contact
'C.Cont = InputBox("Enter a title for this contact.")
C.Cont = frmAddCont.txtTitle.Text
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
'frmStart.lstContact.Items.Add(C.Cont.ToString)
frmStart.lstContact.Items.Add(C)
End Sub
End Class
AddContFrm
Public Class frmAddCont
Public Class ControlObject
Dim Title As String
Dim FName As String
Dim SName As String
Dim Address As String
Dim TelephoneNumber As Integer
Dim emailAddress As String
Dim Website As String
Dim Photograph As String
End Class
Private Sub btnConfirmAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmAdd.Click
Dim C As New Contact
C.Display()
Me.Hide()
End Sub
Private Sub frmAddCont_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
and frmStart.vb
Public Class frmStart
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
frmAddCont.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Dim DelCont As Contact
DelCont = Me.lstContact.SelectedItem()
lstContact.Items.Remove(DelCont)
End Sub
Private Sub lstContact_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContact.SelectedIndexChanged
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim C As Contact
If lstContact.SelectedItem IsNot Nothing Then
C = DirectCast(lstContact.SelectedItem, Contact)
C.Display()
End If
End Sub
End Class
You haven't really added a question but looking at your code it's a bit weird.
If you click Add it will show frmAddCont and then in the confirm button of that form it'll save the data, but if you click Edit it won't show the form and will only add the same data again. I think you're missing a frmAddCont.Show() in your edit button handler.
However, all in all, you're mixing data with GUI too much. The Contact class should know nothing about frmAddCont, rather, the Add and Edit buttons in the main form should show frmAddCont as required (but I would do ShowDialog rather than Show to make it Modal) and if it's in edit mode I'd send in the Contact to be edited to frmAddCont and then when the user press confirm I'd amend/create the Contact as needed and if it's an Add I'd have a method that the main form could call to get out the new Contact.
I think it's fine for the GUI to know about your Contact class, but the Contact class should now know anything about the forms.

VB.NET handling data between different forms

I'm writing a simple application - address book. User enters new addresses and they are added as an entry to a list visible on the main form (frmStart). I use one form to add and edit (AddContForm). Add button on the frmStart works fine, however I experience some problems with the edit button as when I press it and enter new data they are added as new entry however the previous entry is still there. Logic is handled by Contact.vb class. Please let me know how to fix this problem. Here is the code:
Contact.vb
Public Class Contact
Public Contact As String
Public Title As String
Public Fname As String
Public Surname As String
Public Address As String
Private myCont As String
Public Property Cont()
Get
Return myCont
End Get
Set(ByVal value)
myCont = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.Cont
End Function
Public Sub Display()
Dim C As New Contact
C.Cont = frmAddCont.txtTitle.Text
C.Fname = frmAddCont.txtFName.Text
C.Surname = frmAddCont.txtSName.Text
C.Address = frmAddCont.txtAddress.Text
frmStart.lstContact.Items.Add(C)
End Sub
End Class
Here is frmStart.vb
Public Class frmStart
Public Button As String
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Button = ""
Button = "Add"
frmAddCont.ShowDialog()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
Button = ""
Button = "Del"
Dim DelCont As Contact
DelCont = Me.lstContact.SelectedItem()
lstContact.Items.Remove(DelCont)
End Sub
Private Sub lstContact_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContact.SelectedIndexChanged
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Button = ""
Button = "Edit"
Dim C As Contact
If lstContact.SelectedItem IsNot Nothing Then
C = DirectCast(lstContact.SelectedItem, Contact)
frmAddCont.ShowDialog()
End If
End Sub
End Class
Here is AddContFrm.vb
Public Class frmAddCont
Public Class ControlObject
Dim Title As String
Dim FName As String
Dim SName As String
Dim Address As String
Dim TelephoneNumber As Integer
Dim emailAddress As String
Dim Website As String
Dim Photograph As String
End Class
Private Sub btnConfirmAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmAdd.Click
Dim B As String
B = frmStart.Button
Dim C As New Contact
C.Display()
Me.Hide()
If B = "Edit" Then
C = DirectCast(frmStart.lstContact.SelectedItem, Contact)
frmStart.lstContact.SelectedItems.Remove(C)
End If
End Sub
Private Sub frmAddCont_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Update:
I really don't care much for whether the data on the AddForm is the old data or the new data. I don't understand how this code would help me achieve my goal. I think the problem now is that when I click edit previous entry on the list is not deleted (what I believe I have indicated in my question) - I tried to delete it (by using the if condition and checking what button was pressed) however it seems it is not working properly for some reason. I don't understand how your code could help me - please if possible explain it better.
If you're going to use the shared instance of frmAddCont, then it'll remember the data each time you show it, so you'd have to add some kind of Initialize method where you initialize all the controls to the defaults you want (for example empty textboxes).
However, the other way around it would be to do something like:
using(dlg as new frmAddCont)
{
If(dlg.ShowDialog() = DialogResult.OK)
{
Dim C As Contact = dlg.GetNewContact()
}
}
Please note, the above code is not tested and is just meant as a sample rather than the exact code you need.