Separating Chart Series - vb.net

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

Related

How to make All columns except one a datagridview column

I have a datagridview and it looks like this
Datagridview Column
My question is how can I make all columns as checkbox column except the month column?
Here is my code so far
Dim con1 As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=admin1950;database=inventory")
Dim sql1 As MySqlCommand = New MySqlCommand("Select * from period_closure", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
DataGridView1.DataSource = ds1.Tables(0)
con1.Close()
Me.DataGridView1.Columns(0).Frozen = True
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next i
I tried this code
For Each row in DatagridviewRow in Datagridview1.rows + 1
'+1 so the column month will not be affected
dim check as checkboxcolumn
row = checkboxcolumn
next
Its not working
TYSM for help
Add this line after the DataSet has been filled with columns but before binding the data to the DataSet
dtSet.Tables(0).Columns(0).DataType = GetType(Boolean)

Add Column to DataTable before exporting to Excel file

I have a DataTable that is built like this:
Dim dt As New DataTable()
dt = SqlHelper.ExecuteDataset(connString, "storedProcedure", Session("Member"), DBNull.Value, DBNull.Value).Tables(0)
Then it is converted to a DataView and exported to Excel like this:
Dim dv As New DataView()
dv = dt.DefaultView
Export.CreateExcelFile(dv, strFileName)
I want to add another column and fill it with data before I export to Excel. Here's what I'm doing now to add the column:
Dim dc As New DataColumn("New Column", Type.GetType("System.String"))
dc.DefaultValue = "N"
dt.Columns.Add(dc)
Then to populate it:
For Each row As DataRow In dt.Rows
Dim uID As Integer = Convert.ToInt32(dt.Columns(0).ColumnName)
Dim pID As String = dt.Columns(0).ColumnName
Dim qry As String = "SELECT * FROM [MyTable] WHERE [UserID] = " & uID & " AND [PassID] = '" & pID & "'"
Dim myCommand As SqlCommand
Dim myConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connString"))
myConn.Open()
myCommand = New SqlCommand(qry, myConn)
Dim reader As SqlDataReader = myCommand.ExecuteReader()
If reader.Read() Then
row.Item("New Column") = "Y"
Else
row.Item("New Column") = "N"
End If
Next row
But I get a "System.FormatException: Input string was not in a correct format." error when I run the app. It doesn't seem to like these lines:
Dim uID As Integer = Convert.ToInt32(dt.Columns(0).ColumnName)
Dim pID As String = dt.Columns(0).ColumnName
I think I have more than one issue here because even if I comment out the loop that fills the data in, the column I created doesn't show up in the Excel file. Any help would be much appreciated. Thanks!
EDIT:
Okay, I was grabbing the column name instead of the actual data in the column... because I'm an idiot. The new column still doesn't show up in the exported Excel file. Here's the updated code:
Dim dt As New DataTable()
dt = SqlHelper.ExecuteDataset(connString, "storedProcedure", Session("Member"), DBNull.Value, DBNull.Value).Tables(0)
Dim dc As New DataColumn("New Column", Type.GetType("System.String"))
dc.DefaultValue = "N"
dt.Columns.Add(dc)
'set the values of the new column based on info pulled from db
Dim myCommand As SqlCommand
Dim myConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connString"))
Dim reader As SqlDataReader
For Each row As DataRow In dt.Rows
Dim uID As Integer = Convert.ToInt32(row.Item(0))
Dim pID As String = row.Item(2).ToString
Dim qry As String = "SELECT * FROM [MyTable] WHERE [UserID] = " & uID & " AND [PassID] = '" & pID & "'"
myConn.Open()
myCommand = New SqlCommand(qry, myConn)
reader = myCommand.ExecuteReader()
If reader.Read() Then
row.Item("New Column") = "Y"
Else
row.Item("New Column") = "N"
End If
myConn.Close()
Next row
dt.AcceptChanges()
Dim dv As New DataView()
dv = dt.DefaultView
Export.CreateExcelFile(dv, strFileName)
I suppose that you want to search your MyTable if it contains a record with the uid and the pid for every row present in your dt table.
If this is your intent then your could write something like this
Dim qry As String = "SELECT UserID FROM [MyTable] WHERE [UserID] = #uid AND [PassID] = #pid"
Using myConn = New SqlConnection(ConfigurationSettings.AppSettings("connString"))
Using myCommand = new SqlCommand(qry, myConn)
myConn.Open()
myCommand.Parameters.Add("#uid", OleDbType.Integer)
myCommand.Parameters.Add("#pid", OleDbType.VarWChar)
For Each row As DataRow In dt.Rows
Dim uID As Integer = Convert.ToInt32(row(0))
Dim pID As String = row(1).ToString()
cmd.Parameters("#uid").Value = uID
cmd.Parameters("#pid").Value = pID
Dim result = myCommand.ExecuteScalar()
If result IsNot Nothing Then
row.Item("New Column") = "Y"
Else
row.Item("New Column") = "N"
End If
Next
End Using
End Using
Of course the exporting to Excel of the DataTable changed with the new column should be done AFTER the add of the column and this code that updates it
In this code the opening of the connection and the creation of the command is done before entering the loop over your rows. The parameterized query holds the new values extracted from your ROWS not from your column names and instead of a more expensive ExecuteReader, just use an ExecuteScalar. If the record is found the ExecuteScalar just returns the UserID instead of a full reader.
The issue was in my CreateExcelFile() method. I inherited the app I'm modifying and assumed the method dynamically read the data in the DataTable and built the spreadsheet. In reality, it was searching the DataTable for specific values and ignored everything else.
2 lines of code later, I'm done.

Getting DevExpress Chart Series Values From Array 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"})

VB.net chart controls and sql server

Problem: I am currently using a vb application(Visual Studio 2012) to query my database (SQL Server 2012) and display the information using the chart control feature in vb.net
Additional information: I want the x axis to display location name and y axis to display the count of the location
I have complied the code and cannot seem to find the error in the line of code. Please find the code below!
Code:
My code is as follows:
Dim cnn3 As New SqlConnection
Dim cmd3 As New SqlCommand
cnn3.ConnectionString = ("Data Source=SARAHSCOMPUTER;Initial Catalog=FYPDB1;Integrated Security=True")
cmd3.Connection = cnn3
Dim tblFields As String = "SELECT * from tblTagInfo"
Dim oData As New SqlDataAdapter(tblFields, cnn3)
Dim ds As New DataSet
Dim oCmd As New SqlCommand(tblFields, cnn3)
cnn3.Open()
oData.Fill(ds, "tblTagInfo")
cnn3.Close()
Chart1.DataSource = ds.Tables("tblTagInfo")
Dim Series1 As Series = Chart1.Series("Series1")
Series1.Name = "Location"
Chart1.Series(Series1.Name).XValueMember = "Location"
Chart1.Series(Series1.Name).YValueMembers = "SELECT COUNT (Area) FROM tblLocation group by Location"
Chart1.Size = New System.Drawing.Size(780, 350)
End Sub
Please try this code:
Dim cnn3 As New SqlConnection
Dim cmd3 As New SqlCommand
cnn3.ConnectionString = ("Data Source=SARAHSCOMPUTER;Initial Catalog=FYPDB1;Integrated Security=True")
cmd3.Connection = cnn3
Dim tblFields As String = "SELECT COUNT(Location) AS LocationCount, Location AS LocationName FROM tblTagInfo group by Location"
Dim oData As New SqlDataAdapter(tblFields, cnn3)
Dim ds As New DataSet
Dim oCmd As New SqlCommand(tblFields, cnn3)
cnn3.Open()
oData.Fill(ds, "tblTagInfo")
cnn3.Close()
Chart1.DataSource = ds.Tables("tblTagInfo")
Dim Series1 As Series = Chart1.Series("Series1")
Series1.Name = "Sales"
Chart1.Series(Series1.Name).XValueMember = "LocationName"
Chart1.Series(Series1.Name).YValueMembers = "LocationCount"
Chart1.Size = New System.Drawing.Size(780, 350)

vb.net... What am i doing wrong working with access

My probelm is at da.Update(dt). I recieve OleDbException was unhandled. Syntax error in INSERT INTO statement. The weird thing is it ran before when I was only saving 10 items and now it doesn't run at all quite confused. Thanks for any assistance.
Dim dt As New DataTable
Dim ds As New DataSet
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\MyComplete.accdb;Persist Security Info=False;"
con.Open()
MsgBox("here")
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM DealerReview", con)
da.Fill(dt)
Dim newRow As DataRow = dt.NewRow
newRow.Item(1) = User
newRow.Item(2) = Associate
newRow.Item(3) = "1"
newRow.Item(4) = Time
newRow.Item(5) = Hold
newRow.Item(6) = GreetingR
newRow.Item(7) = GreetingA
newRow.Item(8) = GreetingO
newRow.Item(9) = GreetingTs
newRow.Item(10) = GreetingG
newRow.Item(11) = holdUpdate
newRow.Item(12) = LookupSize
newRow.Item(13) = DlyD
newRow.Item(14) = SiPrice
newRow.Item(15) = SiDoorPrice
newRow.Item(16) = TBrand
newRow.Item(17) = TModel
newRow.Item(18) = SeveralChoices
newRow.Item(19) = Financing
newRow.Item(20) = Benefits
newRow.Item(21) = Apt
newRow.Item(22) = ITime
newRow.Item(23) = AssociateScore
newRow.Item(24) = hms
newRow.Item(25) = ymd
newRow.Item(26) = ElapsedTime
dt.Rows.Add(newRow)
Dim cb As New OleDbCommandBuilder(da)
cb.GetInsertCommand()
da.Update(dt)
MsgBox("Saved")
con.Close()
This is the error that you get when your Item's count and/or datatype's do not match the column definitions of the table that you are writing to. There are other possibilities, but since you mentioned that you added more items, and you have not shown us the table definition, that seems the most likely possibility.
You cannot just add more items (columns) to a database command, they have to match the table columns that you are reading/writing from/to.