I have a dataset linked with a MYSQL database via tableadpators and a TableAdpaterManager (all autographed through designer view). The visual studio route was setting up a DataSource and then dragging individual tables onto the form.
Now, I can update, insert and delete records in the database if I edit the datagridviews manually. However, if I remove datarows programatically, the rows are not deleted in the database.
The removal code:
Public Shared Sub RemoveDuplicateRowsViaField(dt As DataTable, RowName As String)
Dim rowList As New List(Of String)
Dim dr As DataRow
For i = dt.Rows.Count - 1 To 0 Step -1
dr = dt(i)
If rowList.Contains(dr.Item(RowName)) Then
dt.Rows.Remove(dr)
Else
rowList.Add(dr.Item(RowName))
End If
Next
dt.DataSet.AcceptChanges()
End Sub
And the 'save' code:
Private Sub SaveOrganisationsBT_Click(sender As Object, e As EventArgs) Handles SaveOrganisationsBT.Click
Me.Validate()
Me.Gi_usersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.dbDS)
End Sub
I've tried many things including looking into the tableAdapters in dataset design view, but am at a loss. Can anyone help?
Nevermind. Solved it. It appears to be around the use of AcceptChanges on the datasource. The code below works. I've read that datarow.remove both deletes the row and accepts changes. I'm guessing if changes are accepted then the tableadapter can't discern it needs deleting from its non 'dirty' state, given changes have been accepted, thus clearing the status of the row? Just supposition. The working code:
Public Shared Sub RemoveDuplicateRowsViaField(dt As DataTable, RowName As String)
Dim rowList As New List(Of String)
Dim dr As DataRow
For i = dt.Rows.Count - 1 To 0 Step -1
dr = dt(i)
If rowList.Contains(dr.Item(RowName)) Then
dr.Delete()
Else
rowList.Add(dr.Item(RowName))
End If
Next
End Sub
Related
I have data in a GridView and I need to copy it to a table in a database.
Let's say my first GridView is GridView1; GridView1's database source is subjects_enrolled. After I'm done using GridView1 it's data needs to clear and in addition, clear the data in the source table.
Note: The primary purpose of GridView1 is for displaying data for printing a page, but I want to save or transfer all data in GridView1 to a database table before losing the GridView1 data.
Here is my code so far:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("mcbdatabseConnectionString").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand("SELECT Subject_ID, Subject, Units FROM mcbdatabse.subject_enrolled")
Using sda As New MySqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End If
txtDate.Text = Date.Today.ToString("yyyy-MM-dd")
End Sub
When you doing some unnormal activity about databases you can be sure that you are in a bad plan. So in your case i can not understand why you want to do this.
You have better ways to do this, you can put all data in one table and add a column named 'enrolld' to filter data as you want and on user click you can delete or enrol the record.
But if you want to continue this way, you we have some events in gridview that can help:
RowDeleting, RowUpdating
If a row affected the related event fires and you can get the id of the record:
protected sub GridView1_RowDeleting(byval sender as object, byval e as GridViewDeleteEventArgs)
{
dim a as object = e.Keys["Id"]
'or
dim a as object = e.Keys[0]
}
To access the id of your record and do what you want with this.
I'm trying to insert a new row into students table but its not doing anything.
Heres my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim stAdapter As New StudentSystemDBDataSetTableAdapters.studentsTableAdapter()
Dim stDataset As New StudentSystemDBDataSet()
Dim Row As StudentSystemDBDataSet.studentsRow
Row = stDataset.students.NewstudentsRow
Row.Student_no = "34"
Row.Title = "ert"
Row.Initials = "3434"
Row.Surname = "erwer"
Row.Address = "fgsfd"
Row.Postal_Code = "rwerwe"
Row.Birthdate = "gogid"
Row.Gender = "erwrwer"
stDataset.students.Rows.Add(Row)
stAdapter.Update(stDataset.students)
End Sub
The UpdateCommand, InsertCommand and 'DeleteCommand` control how your data is updated, inserted and deleted. This is SQL that is parameterized and then used by the adapter to save your changed.
If you used the wizard to create your adapter, run it again and pay attention to the parts that make your data updatable. It will create these for you, but you have to check the box that tells it to.
If you did not use the wizard, you will need to create the commands manually as well.
I am making a CMS portal for my company which will fetch records from SQL server using ASP.NET:
My problem is that when I fetch values it only shows the last one. But my need is that it should display one by one values with say 5-10 seconds gap in between here is my code:
Imports System
Imports System.Data.Sql
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim connectionString As String = "Data Source=soemserv;Initial Catalog=data;User Id=master; Password=hushotn;"
Dim conn As New SqlConnection(connectionString)
Public Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
conn.Open()
Dim comm As New SqlCommand("select Queue,[Timing(IST)],[Status at],TAT,[Staffing at] from DTD_LCD_Queue_Status", conn)
Dim reader As SqlDataReader = comm.ExecuteReader
While reader.Read()
lblQueueName.Text = reader("Queue").ToString.Trim
lblTimingIST.Text = reader("Timing(IST)").ToString.Trim
lblStatusAt.Text = reader("Status at").ToString.Trim
lblTAT.Text = reader("TAT").ToString.Trim
lblStaffingAt.Text = reader("Staffing at").ToString.Trim
End While
End Sub
End Class
How do I loop though each record I tried Do and Loop but its not working...
Thanks in advance!
EDIT
One Step Ahead with no LUCK!!!!
I have written this code but still getting the last row only
For I As Integer = 0 To 15
lblTemp.Text = I.ToString
Dim comm As New SqlCommand("select Queue,[Timing(IST)],[Status at],TAT,[Staffing at] from DTD_LCD_Queue_Status where SrNo='" + lblTemp.Text + "';", conn)
Dim reader As SqlDataReader = comm.ExecuteReader
While reader.Read()
lblQueueName.Text = reader("Queue").ToString.Trim
lblTimingIST.Text = reader("Timing(IST)").ToString.Trim
lblStatusAt.Text = reader("Status at").ToString.Trim
lblTAT.Text = reader("TAT").ToString.Trim
lblStaffingAt.Text = reader("Staffing at").ToString.Trim
Thread.Sleep(2000)
End While
Next
Note I have dynamically given rows i.e 0 to 15
Please guide me!!
Only the last values are printed because : you are fetching a group of rows(data) through the readr and in first iteration of the while loop the first rows(data) are copied to the controls then for the second iteration the contents in the controls are over written with the second row. This will happens in each iteration hence only the last data are displayed.
- This can be avoided by using
Group of controls which are dynamically created(but not practical and good)
using DataGridView(grid) to display the items How to do this
**The method you ware suggested (using Timer) is also possible but it is not a good programming practice only the last values ware displayed all the others flashed for a duration of 5-10 seconds **
I think this question has been asked already, i just can't seem to find an answer i understand.
I have a program that works with entity framework - code first.
When the program runs and the database is not created yet, it will create it and add some data to it as well.
The data it adds are 3 categories, each customer will get a category assigned when a object of customer is created.
The 3 categories are also databound to a combobox, when a customer is created, the categorie is selected from the combobox
problem: when a customer is created, a copy of the category seems to be created as well, the category copy is added to the table of categories (not suppose to happen in my head) and then of course this copy is also added to the combobox.
I'm having trouble understanding why this is happening and what i did wrong.
I'm also not sure which parts of the code i should post here or where i should be looking for error, although i have a feeling the situation result from a bad database design/class design.
This is my first time working with entity framework
Code for the databindings
Dim DisplayLimos As New BindingSource
context.LimosDb.Load()
DisplayLimos.DataSource = context.LimosDb.Local.ToBindingList()
CbLimos.DataSource = DisplayLimos
CbLimos.DisplayMember = "Info"
Dim DisplayCategories As New BindingSource
context.CategoriesDb.Load()
DisplayCategories.DataSource = context.CategoriesDb.Local.ToBindingList()
CbCategorie.DataSource = DisplayCategories
CbCategorie.DisplayMember = "Naam"
Dim DisplayKlanten As New BindingSource
context.KlantenDb.Load()
DisplayKlanten.DataSource = context.KlantenDb.Local.ToBindingList()
ListBoxKlanten.DataSource = DisplayKlanten
ListBoxKlanten.DisplayMember = "Naam"
Dim displayArrangementen As New BindingSource
context.ArrangementenDb.Load()
displayArrangementen.DataSource = context.ArrangementenDb.Local.ToBindingList()
CbArrangementen.DataSource = displayArrangementen
CbArrangementen.DisplayMember = "Naam"
Code for populating the categories table the second parameter is a class that holds discounts for each category
Dim categorieVip As New KlantenCategorie("Vip", kortingVip)
Dim categorieWedding As New KlantenCategorie("Wedding", kortingWeddingPlanner)
Dim categorieNightLife As New KlantenCategorie("NightLife", kortingConcertenPlanner)
context.CategoriesDb.Add(categorieVip)
context.CategoriesDb.Add(categorieWedding)
context.CategoriesDb.Add(categorieNightLife)
context.SaveChanges()
Code that adds the customer to the database and where the error occurs that a copy of the category is also created
Private Sub BtnKlant_Click(sender As Object, e As EventArgs) Handles BtnKlant.Click
Dim adres As New Adres(TxbStraat.Text, TxbHuisN.Text, CType(TxbPostCode.Text, Integer), TxbGemeente.Text)
Dim klant As New Klant(CType(TxbKlantNr.Text, Integer), TxbVoorNaam.Text, TxbNaam.Text, adres, CType(TxbBtwNr.Text, Integer), CType(CbCategorie.SelectedValue, KlantenCategorie))
Try
_Context.KlantenDb.Add(klant)
_Context.SaveChanges()
ListBoxKlanten.Refresh()
MessageBox.Show("Klant succesvol toegevoegt")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I believe i have found where my mistake lies.
In the part where i create a new customer and pass through the category it should have
Private Sub BtnKlant_Click(sender As Object, e As EventArgs) Handles BtnKlant.Click
Dim adres As New Adres(TxbStraat.Text, TxbHuisN.Text, CType(TxbPostCode.Text, Integer), TxbGemeente.Text)
Dim klant As New Klant(CType(TxbKlantNr.Text, Integer), TxbVoorNaam.Text, TxbNaam.Text, adres, CType(TxbBtwNr.Text, Integer), CType(CbCategorie.SelectedValue, KlantenCategorie))
Try
_Context.KlantenDb.Add(klant)
_Context.SaveChanges()
ListBoxKlanten.Refresh()
MessageBox.Show("Klant succesvol toegevoegt")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I changed this
Dim klant As New Klant(CType(TxbKlantNr.Text, Integer), TxbVoorNaam.Text, TxbNaam.Text, adres, CType(TxbBtwNr.Text, Integer), CType(CbCategorie.SelectedValue, KlantenCategorie))
to this
Dim klant As New Klant(CType(TxbKlantNr.Text, Integer), TxbVoorNaam.Text, TxbNaam.Text, adres, CType(TxbBtwNr.Text, Integer), _Context.CategoriesDb.First(Function(ct) ct.Naam = CbCategorie.Text))
Using a lambda to pull the correct category out of the database and link it to the new customer created. (atleast that is how i understand it)
This link is what i am mostly going on to create this project, one of the few i found on code first EF with actual VB examples.
http://visualstudiomagazine.com/articles/2012/03/07/an-ef-code-first-tutorial.aspx
If anyone wants to add criticism to my way of working or coding, please do!
I am always open to improving my nooby skills :)
I've got a Windows Form with a button and a datagridview. The project includes a working database connection and LINQ to SQL class. I'm trying to bind the datagridview to the LINQ to SQL.
In a code module I've got this:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
Return NewBindingSource
End Function
And this button_click code in the form:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.DataSource = DataGridList()
End Sub
When I click the button I get the length of the school names in the datagridview, with a column header of "length."
If I just run this very similar code in the button_click instead, the school names appear correctly in the immediate window:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim db As New DataClasses1DataContext()
Dim TestQuery =
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
For Each block In TestQuery
Debug.Print(block)
Next
End Sub
Give this a try:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select New With { Key .Value = Block.gr6_school } Distinct
Return NewBindingSource
End Function
This should give it a property that the DataGridView can pick up on. The New With... creates an anonymous object with a Property named Value. The DataGridView works on this type of object by enumerating the public properties and rendering them as columns. If you had needed more than one value, you could have added additional items inside the curly braces in the same way separated by commas. See Anonymous Types (Visual Basic) for more information.
You might try adding a .ToString to the Select:
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school.ToString Distinct
I believe that Debug.Print does an implicit conversion to .ToString when it prints to the immediate window. However, a datagrid cell treats everything as an object and displays the default property for that object.
Turns out, of course, this has been addressed often, including on SO, and here. The route I chose was to use an intermediate DataTable:
Public Function DataGridList() As DataTable
Dim NewDataTable As New DataTable
Dim db As New DataClasses1DataContext()
Dim i As Int32
Dim qry =
From Block In db.BLOCK_ASSIGNMENTs.AsEnumerable
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
NewDataTable.Columns.Add("School")
For i = 0 To qry.Count - 1
NewDataTable.Rows.Add(qry(i))
Next
Return NewDataTable
End Function
This seems pretty slow the first time it runs, so I may try something else in the future, but it allows me to feed the grid via LINQ, which is what I want to work with.
(I would like to use the qry's CopyToDataTable property, but it's only available if the query returns a DataTableRows collection, or some such, and my hacking around didn't reveal how to do that.)