VB.net chart controls and sql server - sql

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)

Related

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"})

How to configure multiple dataadapters?

The following code works ok.
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
But for some reason when I add additional dataadapters, as per the below, I get the following error
"Object reference not set to an instance of an object."
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim sql2 As String = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.[bill] From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" + TextBox1.Text + "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE s.[CLI] is NULL"
Dim comm2 As SqlCommand = New SqlCommand(sql2, conn)
Dim dataadapter2 As SqlDataAdapter = New SqlDataAdapter(comm2)
Dim sql3 As String = "SELECT s.[ID],s.[Site],s.[CLI],s.[CustomerName],s.[FromDate],s.[ToDate],s.[Quantity],s.[UnitCost],s.[TotalCost],s.[Description],s.[filenameonly],s.billingmonth as [CurrentBillingMonth], s.[bill] From [DaisyServices].[dbo].[" + TextBox1.Text + "] s LEFT JOIN [DaisyServices].[dbo].[DaisyServicesIndigo] i on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE i.[CLI] is NULL"
Dim comm3 As SqlCommand = New SqlCommand(sql3, conn)
Dim dataadapter3 As SqlDataAdapter = New SqlDataAdapter(comm3)
How should I configure multiple adapters on the same form?
Thanks

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

how to execute data through label.Text in vb.net

I'm new in vb.net. I'm using vb.net and sql
I had query this "SELECT * FROM sf_OEEShowRed" and how I'm going to show the result in Label1.Text? Below are my code.
Private Function GetRed()
Try
Dim connectionString As String = System.Configuration.ConfigurationManager.AppSettings("StrConn")
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT * FROM sf_OEEShowRed"
Dim sqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
Dim dataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet
Finally
Label1.Text =
End Try
End Function
You can try:
Me.Label1.Text = dataSet.tables(0).defaultview

Updating database from data in Gridview in VB

This is the code for displaying data from database to gridview....If i am doing any changes in GridView and it has to update in database...I dont know how to do it...Can anyone help me....
Dim DBCONSRT, QRYSTR As String
Dim strSQL As String
Dim DBCON, myConn, myCommand, rs As Object
Dim NoOfRecords As Long
rs = CreateObject("ADODB.Recordset")
Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=cpa5k;PASSWORD=pass;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"
myConn = CreateObject("ADODB.Connection")
myCommand = CreateObject("ADODB.Command")
myConn.Open(DB_CONNECT_STRING)
myCommand.ActiveConnection = myConn
strSQL = "select * from QA1MM.STRSK_OH FETCH FIRST 10 ROWS ONLY with ur;"
rs.Open(strSQL, myConn)
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim myDS As DataSet = New DataSet
myDA.Fill(myDS, rs, "MyTable")
DataGridView1.DataSource = myDS.Tables(0)
DataGridView1.Refresh()
myConn.Close()
I am trying this method.but it is not working
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim myDS As DataSet = New DataSet
myDA.Fill(myDS, rs, "MyTable")
ds = DataGridView1.DataSource
myDA.Update(ds)
I changed like this:
Dim DBCONSRT, QRYSTR As String
Dim strSQL As String
Dim DBCON, myConn, myCommand, rs As Object
Dim ds As DataSet = New DataSet
rs = CreateObject("ADODB.Recordset")
Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=cpa5k;PASSWORD=mexico13;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"
myConn = CreateObject("ADODB.Connection")
myCommand = CreateObject("ADODB.Command")
myConn.Open(DB_CONNECT_STRING)
myCommand.ActiveConnection = myConn
Dim myDA As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
Dim myDS As DataSet = New DataSet
Dim dtable As DataTable = New DataTable()
myDA.UpdateCommand = New SqlClient.SqlCommand("UPDATE QA1MM.STRSK_OH set OH_QTY = 10 WHERE SKU_NBR = 108011", myConn)
' myDA.UpdateCommand.Parameters.Add("#OH_QTY", OleDb.OleDbType.VarChar, 15, "OH_QTY")
' myDA.UpdateCommand.Parameters.Add("#SKU_NBR", OleDb.OleDbType.VarChar, 15, "SKU_NBR")
' myDA.UpdateCommand.Parameters(0).SourceVersion = DataRowVersion.Current
' myDA.UpdateCommand.Parameters(1).SourceVersion = DataRowVersion.Current
dtable = DataGridView1.DataSource
myDA.Update(dtable)
myConn.Close()
But it is giving error like "Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.SqlClient.SqlConnection'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."
I changed the connection string like:
Provider=MSDASQL.1;Persist Security Info=False;Data
Source=NP1;DSN=NP1;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Integrated
Security = True;Initial Catalog=QA1MM;
Its not working.The error is "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.".....Sorry i dont know how to change.....
You write like follow
myConn.Open(DB_CONNECT_STRING)
myDA.Update(ds.Tables(0))
myConn.Close()
EDIT:
Dont take the datasource of DataGridView1 into a dataset, instead take into a DataTable as follows
Dim dtable As New DataTable()
dtable = DataGridView1.DataSource
and update the table with DataAdapter as
myConn.Open(DB_CONNECT_STRING)
myDA.Update(dtable)
myConn.Close()
Refer to this link How to write UpdateCommand to OleDBDataAdapter
Try this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataSet1.Tables("employee").AcceptChanges()
Dim i As Integer
Dim cmdbuilder As New OdbcCommandBuilder(odbcadptr)
i = odbcadptr.Update(odbcds, "customer")
MsgBox("Updated Rows: " & i)
End Sub