VB.NET/SQL System.Argument Exception - sql-server

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

Related

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

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 :-)

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_

Getting the connection string from an imported source

I have added a new datasource from a SQL-server into my project and want to create a SQLConnection (using System.Data.SqlClient).
During the datasource creation I saved the Connectionstring as Dev_DBConnectionString and want to use this now but I have no clue how. I tried
ConsoleApplication1.Properties.Settings.Default.masterConnectionString
(this is how some c# tutorials doing it) but get the error: Properties is not a Member of ConsoleApplication1. I have checked some vb.net tutorials (e.g.) but they are using the descriptive way like
Data Source=MSSQL1;Database=AdventureWorks;" & "Integrated Security=true;
But I guess that the string is already somewhere in my system. Anyone who can help me out here?
FIRST
You have to setup your connection!
Imports System.Data.SqlClient
Imports System.Data
Public Class SQLTools
Inherits System.Windows.Forms.Form
Private Const SqlString As String = Dev_DBConnectionString
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private myReader As SqlDataReader
Private Sub Conn()
myConn = New SqlConnection(SqlString)
End Sub
Now you can enjoy your DB connection
It's time to do what you want! you just need to call Conn() to setup everything and than you have to open the connection using MyConn.Open()
Dev_DBConnectionString problems
You can always write it again.
e.g.
Private Const SqlString As String = "Server=****;Database=****;User ID=****;Password=****;Integrated Security=SSPI;"

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 Open image from database to picturebox

I'm trying to open an image from my database to a picture box but I just don't how to do it.
I've searched for some answers and I am not familiar with the codes for I am beginner only.
The only codes that I researched about are for connecting the database to the system:
Imports System.Data.OleDb
Module Module1
Public acsconn As New OleDbConnection
Public acsdr As OleDbDataReader
Public acsda As New OleDbDataAdapter
Public acscmd As New OleDbCommand
Public strsql As String
Public acsds As New DataSet
Public Sub connect()
Try
acsconn.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=|datadirectory|\database1.mdb;"
acsconn.Open()
If acsconn.State = ConnectionState.Open Then
MsgBox("Connected")
Else
MsgBox("Error")
End If
Catch ex As Exception
End Try
End Sub
End Module
I do not know what is next. BTW, those codes - I used it for saving the image to the database.
I think this is the sort of thing you are looking for:
Private Sub HandleRequest(context as HttpContext)
Dim SqlCnn As SqlConnection = Nothing, sql As String
Dim emp_id As Integer
emp_id = Int32.Parse(context.Request.QueryString("id"))
ConnectDB(SqlCnn)
Try
sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
sqlcmd = New SqlCommand(sqlstr, SqlCnn)
Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
Catch ex As Exception
ReportError(ex)
Finally
CloseDB(SqlCnn)
End Try
End Sub

Resources