SqlConnection with VB.NET, is there System.Data.dll missing? - sql-server

With my VBA experience I´m trying VB.NET now. First I need a connection to my MS SQL Server.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connetionString As String
Dim cnn As SqlConnection
connetionString = "Data Source=MyDS;Initial Catalog=MyIC;User ID=MyUser;Password=MyPW"
cnn = New SqlConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
The error: Type "SqlConnection" is not defined.
SqlConnection is also red underlined directly in the Dim cnn - line.
Also there is no intellisense suggestion for it.
I came across System.Data.dll several times on google searches. Is it missing in my system? I tried to import it without success.
Then I searched it in
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5
and
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0
It isn´t there.
Is System.Data.dll my problem?
Win10, Visual Studio Version 17.4.1 (64-Bit)
Maybe I´ve installed Visual Studio incomplete? Or is there an other reason for my problem?
Thank you very much for your time and a hot tip :-)

Related

Problem Opening SQLite Connection in VB.net

I am trying to learn SQLLite (Years of Experience with apps using SQL Server). I am going slowly and right off the bat I could not open a connection to a database created in SQLite Studio. I have tried a number of different examples with no luck. I first tried with a Nuget package and then went to the SQLite website and downloaded the 64-bit binaries for .Net 4.5. I get this error no matter what I try: system.BadImageFormatException.
Can someone please tell me what I'm doing wrong or point me to an example that really works?
Thanks!!!
Imports System.Data.SQLite
Public Class Form1
Private connectionstring As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim exePath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
Dim dbPath As String = exePath & "\NamesTest.db"
connectionstring = "Data Source=" & dbPath
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SQLiteConn As New SQLiteConnection(connectionstring)
SQLiteConn.Open()
End Sub
End Class

VB.NET insert data via form

I am trying to insert data to database, but getting error as ExecuteNonQuery: Connection property has not been initialized.
Everything seems ok with the connection.
Imports System.Data.SqlClient
Public Class crud1
Private Sub Add_btn_Click(sender As Object, e As EventArgs) Handles Add_btn.Click
Try
Dim conn_ As New SqlConnection("Data Source=DESKTOP-VVM5A31;Initial Catalog=temp;Integrated Security=True")
conn_.Open()
Dim command As New SqlCommand("INSERT INTO STUDENT (student_name, student_age) VALUES (#NAME, #AGE)")
command.Parameters.AddWithValue("#NAME", TXT_NAME.Text)
command.Parameters.AddWithValue("#AGE", Convert.ToInt32(TXT_AGE.Text))
command.ExecuteNonQuery()
MsgBox("Record saved.", MsgBoxStyle.Information)
conn_.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
Please see the image for the connection property.
You are right taking into account that the command needs the connection open, before execute the command, and you are doing right.
But you are not telling to the command which is the connection to be used.
An easy way to do it would be something like this, before execute the command:
command.Connection = conn_

vb.net crystal report is asking for database login and password but logon failed

I'm new in making crystal reports and I'm done creating my crystal report but when running my program it asks for my logon information but after I log in, it says that
logon failed
I tried to look in other questions here in the site but I was not able to find an answer. My Visual Studio is 2013 Ultimate and my sql is sql-server 2014 express.
Here's my code:
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim show As String = String.Empty
show &= "select * from fruit_stock "
show &= "where date_received=#daterec"
Using conn As New SqlConnection("server=WIN10;database=fruit_storage;user=fruit_admin;password=admin;")
Using cmd As New SqlCommand
With cmd
.Connection = conn
.CommandType = CommandType.Text
.CommandText = show
.Parameters.AddWithValue("#daterec", TextBox1.Text)
End With
Try
conn.Open()
Dim da As New SqlDataAdapter
Dim ds As New DataSet
da.SelectCommand = cmd
da.Fill(ds, "fruit_stock")
Dim report As New CrystalReport1
CrystalReportViewer1.ReportSource = report
CrystalReportViewer1.Refresh()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
End Class
Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
report.Load("<physical filename of your report>")
report.SetDataSource(ds.Tables("fruit_stock"))
<your_crystal_report_viewer_in_your_form>.ReportSource = report
if you are using an embedded report
Dim report As New <Name of your embedded crystal report>
report.SetDataSource(ds.Tables("fruit_stock"))
<your_crystal_report_viewer_in_your_form>.ReportSource = report

database connection with a chart

I have been trying to figure this out for a few days but every time I try and fix it I encounter the same error. I am trying to implement the data from my database into a candlestick style chart.Every time I run it i'm faced with this same error:
"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."
I have used a combination of the limited resources given to me and code that I found here on a previously answered question to create the following code.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim myConnection As OleDbConnection = New OleDbConnection
Dim provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Dim dataFile As String = "C:\Users\Ian\Documents\A2 Woking\Computing\Database1\Database_data_source .accdb"
Dim DatabasePass As String = "DatabasePW"
Dim connString As String = provider & dataFile
Dim execute As OleDbDataReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myConnection.ConnectionString = connString
Dim objDataAdapter As OleDbCommand = New OleDbCommand("Select Time, Open, High, Low, Last FROM EUR_USD_TB", myConnection)
myConnection.Open()
execute = objDataAdapter.ExecuteReader
While execute.Read
chartTemplate.Series("dataSeries").Points.AddXY(execute("Time"), execute("Low"), execute("High"), execute("Open"), execute("Last"))
End While
' Close the reader and the connection
execute.Close()
myConnection.Close()
End Sub
End Class
Any help would be appreciated i'm sure its something annoyingly simple I don't get.
The path is correct---
The database is not protected by a password, that variable is for later---
The query works in access---
This same code works perfectly for accessing a separate table in my login

VB.NET/SQL System.Argument Exception

I am brand new to programming, and I have been encountering several errors as I work to build an application. The additional information section of the Visual Studio error box delivers the following message:
An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: Format of the initialization string does not conform to specification starting at index 0.
This occurs as the application attempts to execute the following line of code:
Dim da As New SqlDataAdapter(sql, cs)
I have been working to troubleshoot this to no avail. Thanks for any help you are kind enough to provide! Please find the additional info/code for the class below:
Imports System.Data
Imports System.Data.SqlClient
Public Class DButil
Public cs As String
Public Function GetDataView(ByVal sql As String) As DataView
Dim ds As New DataSet
Dim da As New SqlDataAdapter(sql, cs)
da.Fill(ds)
Dim dv As New DataView(ds.Tables(0))
Return dv
End Function
Public Sub New()
cs = "Data Source=(LocalDB)\v11.0"
cs += "Data Source=(LocalDB)'C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True;"
cs += "Integrated Security =True;Connect Timeout=30"
End Sub
End Class
Thanks for the reply, Steve. That removed the error from the following line: Dim da As New SqlDataAdapter(sql, cs). An error now appears on the following line: da.Fill(ds). This error says SqlException unhandled, and that an expression of non-boolean type where a condition is expected near ",". Thoughts? –
Your connection string is really wrong.
For Sql Server 2012 with LocalDB instance you need
Public Sub New()
cs = "Server=(LocalDB)\v11.0;"
cs += "Integrated Security=True;"
cs += "AttachDbFileName=C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf;"
End Sub
See examples of connectionstrings for Sql Server at connectionstrings.com
Your connection string is definitely wrong. Have a look at http://www.connectionstrings.com/sqlconnection/localdb-automatic-instance-with-specific-data-file/
Here is an example of how to query your database and return a dataview
Public Function GetDataView(sql As String) As DataView
Dim cs = "Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf;"
Using cnn As New SqlConnection(cs)
Using cmd As New SqlCommand(sql, cnn)
Try
cnn.Open()
Dim t As New DataTable
t.Load(cmd.ExecuteReader)
Return New DataView(t)
Catch ex As Exception
''handle the error
End Try
End Using
End Using
End Function

Resources