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
Related
I'm using the Web service as given below for getting the data from database. In that code it has static string connection and string query. I'm using the same web service for multiple functions. Instead of using the same function I need to give the string query and string connection manually to avoid multiple of using the same code. Is it possible to give the string connection and string query in run time.
Here is the code.
<WebMethod()> _
Public Function GetData() As String
Dim sqlcon As New SqlClient.SqlConnection
Dim sqlcmd, cmd As SqlClient.SqlCommand
Dim objeDS, objeDS1 As DataSet
Dim adpp, adpp1 As SqlClient.SqlDataAdapter
Dim dt, dt1 As DataTable
Dim pack_tbl As New DataTable
sqlcon.ConnectionString = ConfigurationManager.AppSettings("constr")
sqlcon.Open()
sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = sqlcon
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = "sqlcommand"
objeDS = New DataSet()
adpp = New SqlClient.SqlDataAdapter()
adpp.SelectCommand = sqlcmd
adpp.Fill(objeDS)
sqlcon.Close()
dt = New DataTable()
dt = objeDS.Tables(0)
Return objeDS.GetXml()
'Return "Welcome"
End Function
Try this code this helps you.
VB code
<WebMethod()> _
Public Function GetData(ByVal strQuery As String, ByVal strCon As String) As String
Dim dt As New DataTable()
dt.Clear()
Using conn As New System.Data.SqlClient.SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings(strCon).ConnectionString
Using cmd As New System.Data.SqlClient.SqlCommand()
cmd.CommandText = strQuery
cmd.Connection = conn
conn.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Using
End Using
End Function
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
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
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)
The result of my sql-query is a single value i want display that in a lable but dont know how. I have a solution creating a dataset put the result of the query into that dataset and get it by row(0),column(0) but i guess there is a smarter way doing it.
Private myquery As String
Sub New(ByVal query As String)
myquery = query
End Sub
Public Sub query_Send()
Dim myconn As New SqlConnection("Data Source=DB;Integrated Security=TRUE")
Dim mycmd As SqlCommand = New SqlCommand(Me.myQuery, myconn)
Dim myTable As New DataTable
Dim previousConnectionState As ConnectionState = myconn.State
Try
If myconn.State = ConnectionState.Closed Then
myconn.Open()
Dim myDA As New SqlDataAdapter(mycmd)
myDA.Fill(myTable)
tb.text = **...**
End If
If previousConnectionState = ConnectionState.Closed Then
myconn.Close()
End If
End Try
End Sub
You can use SqlCommand.ExecuteScalar Method
Dim querystr as String = "select value from MyTable where ID=5"
Dim mycmd As New SqlCommand(querystr, connection)
dim value as Object = mycmd.ExecuteScalar()
The value you can put to your textbox. ExecuteScalar returns first value of first row of the resultset (equivalent of row(0).column(0) ) When no record is returned the value is nothing.
Try something more along the lines of:
Using myconn As New SqlConnection("Data Source=DB;Integrated Security=TRUE")
Dim myDA As New SqlDataAdapter()
myDA.SelectCommand = New SqlCommand(myQuery, myconn)
myDA.Fill(myTable)
Return myTable
End Using
Or you can just get ahold of the first item in the first row with
myTable.Rows(0).ItemArray(0).ToString()