Getting DevExpress Chart Series Values From Array VB.NET - vb.net

I was trying to set DevExpress Chart Series from Data inside SQL Table.
Everything went fine except that the chart takes only the last attribute from the last series.
My code is:
con.Open() 'it opens SQL Connection
For i = 0 To (CheckedListBoxControl1.CheckedItems.Count - 1) 'list from ListBox
Lst.Add(CheckedListBoxControl1.CheckedItems(i).ToString) 'Putting Data in Array List
Dim Strl As String = String.Format("Select * from VPRogressCumulative where fname like '{0}' and lname like '{1}' order by id, no, CAST('1.' + date AS datetime)", ComboBox1.Text, Lst(i))
Dim sqlCom As New SqlCommand(Strl)
sqlCom.Connection = con
Dim myDA As SqlDataAdapter = New SqlDataAdapter(sqlCom)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "VPRogressCumulative")
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Next
con.Close()
I believe my Problem is in:
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Last two lines are giving the same series new attributes which I wasn't able to resolve the issue.

Your problem is here:
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
In each iteration of cycle you are creating the new DataSet and it's overrides previous DataSource in your ChartControl1. Your must use Series.DataSource instead of ChartControl.DataSource. Also you can use DataTable instead of DataSet.
Here is example:
'Your code:
'Dim myDataSet As DataSet = New DataSet()
'myDA.Fill(myDataSet, "VPRogressCumulative")
'ChartControl1.DataSource = myDataSet
'ChartControl1.DataAdapter = myDA
'Replace with this:
Dim myDataTable As DataTable = New DataTable("VPRogressCumulative")
myDA.Fill(myDataTable)
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
'Your code.
'ser.ArgumentDataMember = "VPRogressCumulative.Date"
'ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
'Replace with this:
ser.DataSource = myDataTable
ser.ArgumentDataMember = "Date"
ser.ValueDataMembers.AddRange(New String() {"Cumulative"})

Related

Dataset to a table in database

I have a Dataset called 'ds' and I want to put that dataset into one of my tables in my sql database. I cannot seem to find a solution for this. How would I go about putting the dataset into a table? Any help would be greatly appreciated!
Code so far. Dataset that is created from user input:
Dim ds As DataSet = CreateNewDataSet()
Dim entries As New List(Of String())
Console.WriteLine("Enter first name: ")
firstName = Console.ReadLine()
Console.WriteLine("Enter last name: ")
lastName = Console.ReadLine()
entries.Add({firstName, lastName})
entries.ForEach(Sub(x) ds.Tables(0).Rows.Add(x))
I'm only using one table, 'Person' with the fields 'firstname' and 'lastname'.
Trying to insert dataset into table Person:
Using connection = New OleDbConnection("connectionstring")
connection.Open()
Dim adapter = New OleDbDataAdapter()
Dim myQuery As String = String.Empty
myQuery &= "INSERT INTO Applicant (strFirstName, strLastName)"
myQuery &= "VALUES(?, ?)"
adapter.InsertCommand = New OleDbCommand(myQuery, connection)
adapter.InsertCommand.Parameters.AddWithValue("#strFirstName", firstName)
adapter.InsertCommand.Parameters.AddWithValue("#strLastName", lastName)
adapter.Update(ds)
End Using
This is the solution:
Private Function CreateObject(ds as DataSet)
Dim connection as OdbcConnection = new OdbcConnection(conString)
connection.Open()
Dim adapter as OdbcDataAdapter = new OdbcDataAdapter("select * from Table", connection)
ds = new DataSet("DefaultName")
adapter.FillSchema(ds, SchemaType.Source, "Table")
adapter.Fill(ds, "Table")
Dim table as DataTable
table = ds.Tables("Table")
Dim row as DataRow = Nothing
row = table.NewRow()
row("field1") = varField1
row("field2") = varField2
table.Rows.Add(row)
Return ds
End Function

Separating Chart Series

I currently have a chart element that has 4 Series. My problem is that while they work individually, if I set more than 1 Series it sets the data for all of them to the last DataSet (So Series 1,2 & 3 have the same positions as 4).
Could someone have a look at my broken code and let me know where it's all going wrong? And maybe some pointers on neatening it up... I have never worked with any form of charts before.
Using con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Response.mdb;")
Dim MyQuery As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Happy'));"
Using cmd = New OleDbCommand(MyQuery, con)
Dim MyData As New OleDbDataAdapter(MyQuery, con)
Dim MyDataSet As New DataSet
con.Open()
MyData.Fill(MyDataSet, "Table")
ChrtMoodChanges.DataSource = MyDataSet.Tables("Table")
Dim Series1 As Series = ChrtMoodChanges.Series("Series1")
Series1.Name = "Happy"
ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series1.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery2 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Neutral'));"
Using cmd = New OleDbCommand(MyQuery2, con)
Dim MyData2 As New OleDbDataAdapter(MyQuery2, con)
Dim MyDataSet2 As New DataSet
MyData2.Fill(MyDataSet2, "Table")
ChrtMoodChanges.DataSource = MyDataSet2.Tables("Table")
Dim Series2 As Series = ChrtMoodChanges.Series("Series2")
Series2.Name = "Neutral"
ChrtMoodChanges.Series(Series2.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series2.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery3 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Sad'));"
Using cmd = New OleDbCommand(MyQuery3, con)
Dim MyData3 As New OleDbDataAdapter(MyQuery3, con)
Dim MyDataSet3 As New DataSet
MyData3.Fill(MyDataSet3, "Table")
ChrtMoodChanges.DataSource = MyDataSet3.Tables("Table")
Dim Series3 As Series = ChrtMoodChanges.Series("Series3")
Series3.Name = "Sad"
ChrtMoodChanges.Series(Series3.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series3.Name).YValueMembers = "CountOfMood"
End Using
Dim MyQuery4 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Angry'));"
Using cmd = New OleDbCommand(MyQuery4, con)
Dim MyData4 As New OleDbDataAdapter(MyQuery4, con)
Dim MyDataSet4 As New DataSet
MyData4.Fill(MyDataSet4, "Table")
ChrtMoodChanges.DataSource = MyDataSet4.Tables("Table")
Dim Series4 As Series = ChrtMoodChanges.Series("Series4")
Series4.Name = "Angry"
ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series4.Name).YValueMembers = "CountOfMood"
End Using
End Using
P.S. the SQL returns the correct data for each... it's just getting it to store the correct data in the charts is my problem.
Everytime you set the ChrtMoodChanges.DataSource you are overwriting the datasource for the chart, thats why you end up with only the last dataset.
I would suggest you merge your datatables before you start binding them to the chart then reference each series to a column in your datasource/datatable
ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series1.Name).YValueMember = "CountOfMood_Col1"
....
....
ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series4.Name).YValueMember = "CountOfMood_Col4"
So as a suggestion to cleaning up the code, I would do the below
Get the 4 datatables
Merge the datatables with "Actual_Date" as the primary key
add each column to the chart as a new series
Merging Example: Merge columns of different types from datatables into one larger datatable
Hope this helps

Select with condition from a datatable in VB.net

I want to select a certain field from a datatable in VB based on the value of another field in the same row.
In SQL, it would easily be done by writing this query:
select error_message from table_errors where error_case="condition"
How do I do this if I have my SQL table filled in a datatable in VB?
How do I select the item("error_message") in the datatable based on the item("error_Case") field?
Any help would be appreciated
You can use Linq-To-DataSet:
Dim matchingRows As IEnumerable(Of DataRow) =
From row In table
Where row.Field(Of String)("error_case") = "condition"
If you just want one column (of course that works also in one step):
Dim errorMessages As IEnumerable(Of String) =
From row In matchingRows
Select row.Field(Of String)("error_message")
For Each error In errorMessages
Console.WriteLine(error)
Next
If you expect it to be just a single row use First or Single(throws an exception if there is more than one row):
Dim error As String = errorMessages.First()
Since First throws an exception if the sequence is empty you can use FirstOrDefault:
Dim error As String = errorMessages.FirstOrDefault() ' is null/Nothing in case of an empty sequence
All in one line (note that both Linq and DataTable.Select needs to use loops):
Dim ErrMessage As String = errorTable.AsEnumerable().
Where(Function(r) r.Field(Of String)("Error_Case") = TextCase.Text).
Select(Function(r) r.Field(Of String)("Error_Message")).
FirstOrDefault()
here is a worker version of the rough code
Dim connString As String = "select error_message from table_errors where error_case='condition'"
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(connString)
conn.Open()
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(connString, conn)
Dim dTable As DataTable = New DataTable()
Dim dAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd)
dAdapter.Fill(dTable)
conn.Close()
Dim text As String
Dim dReader As DataTableReader = dTable.CreateDataReader()
While dReader.Read()
text = dReader.GetValue(0)
End While
dReader.Close()

Copy full row from a table's database

I'm using VB.Net and I have a database contains two tables "Personne" and "PersonneCopy" ( Same schema as Personne [4 columns = N°, FirstName, LastName, B_Day])
I load the first table in a Datagridview all it's fine with that, here's the code:
Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= .\BD.mdb")
Dim SqlRequete As String = "Select * From Personne"
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim DataSet As New DataSet
Adapter.Fill(DataSet, "Tb1")
Dim DataTable As New DataTable
DataTable = DataSet.Tables("Tb1")
DataGridView1.DataSource = DataTable
Is there any way to add the full row that was selected from DataGridView1 to the table "PersonneCopy"?
And by the way I tried this code with an other Datagridview to get the index of the row selected and it works.
Dim Index As Integer = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value
Dim SqlRequete As String = "Select * From Personne where N°=" & Index.ToString
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim Ds As New DataSet
Adapter.Fill(Ds, "Tb")
DataGridView2.DataSource = Ds.Tables(0)
Try like this
Dim drCopy as DataRow
Dim tbCopy as Datatable
tbCopy = .... 'PersonneCopy table
drCopy=tbCopy.NewRow()
For i as integer=0 to drCopy.columns.count-1
drCopy(i)=Ds.Tables(0).rows(0).items(i) '---------> or you can change rows(0)
Next
tbCopy.Rows.Add(drCopy)

Saving record with dataset

I want to save a record in the database using a dataset, but my data is not committing into my database.
My code can be viewed below:
Dim mydataset1 As New MyDataSet
Dim row As DataRow = mydataset1.Tables("testtable").NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
mydataset1.Tables("testtable").Rows.Add(row)
Any help will be appreciated
A DataSet/DataTable is a offline/in-memory representation of your database. If you want to update the database, you need to use a DataAdapter.
For example (assuming you're using MS-Sql-Server):
Public Function UpdateDataSet(dataSet As DataSet) As Int32
Using con = New SqlConnection(My.Settings.SqlConnection)
Dim sql = "INSERT INTO TUser(Name,Address)VALUES(#Name,#Address)"
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
cmd.Parameters.Add(New SqlParameter("#Address", SqlDbType.VarChar))
Using da = New SqlDataAdapter()
da.InsertCommand = cmd
con.Open()
Dim rowCount = da.Update(dataSet)
Return rowCount
End Using
End Using
End Using
End Function
I could be rusty here since its a long time since I wrote any VB.NET or used data adapters/datasets/datatables but I think if you decide to take that route you would need code like this:
Dim connection As New SqlConnection("#####YourConnectionString#####")
connection.Open()
Dim adapter As New SqlDataAdapter("SELECT * FROM testtable", connection)
' For the line below to work, you must have a primary key field in "testtable"
Dim builder As New SqlCommandBuilder(adapter)
Dim testtable As New DataTable("testtable")
adapter.Fill(testtable)
Dim row As DataRow = testtable.NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
testtable.Rows.Add(row)
adapter.Update(testtable)
connection.Close()