473,388 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Connection Property has not been initialized


I am new to VB.Net and I am trying to create a program that inserts data into a SQL table. Below you will find my code that gives me the following error: Connection Property has not been initialized

I cannot see (from inexperience) where I am having the issue.

Many thanks!!!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand

Dim Conn = New SqlClient.SqlConnection
Conn.ConnectionString = "Data Source=[ServerName]; Initial Catalog=SenTest; password=""[Password]""; persist security info =True; user id=sa; workstation ID=Man1; Packet Size =4096"
Dim InsertQuery As String

daBill.SelectCommand = cmdSelectBill
daBill.UpdateCommand = cmdUpdateBill
daBill.InsertCommand = cmdInsertBill
daBill.DeleteCommand = cmdDeleteBill

daBill.SelectCommand.CommandText = "Select * from Bill"
daBill.SelectCommand.Connection = Conn

daBill.InsertCommand.CommandText = "Insert Into Bill(BillNumber, Description) Values (@BillNumber, @Description)"
daBill.InsertCommand.Connection = Conn

cmd.Parameters.Add("@BillNumber", SqlDbType.Char, 10, "@BillNumber")
cmd.Parameters.Item("@BillNumber").Value = TextBox1.Text

cmd.Parameters.Add("@Description", SqlDbType.Char, 1000, "@Description")
cmd.Parameters.Item("@Description").Value = TextBox2.Text

cmd.Connection = Conn
InsertQuery = cmd.ExecuteNonQuery

End Sub
Dec 11 '06 #1
7 33517
4,871 Expert 4TB
I believe the problem is the use of the conn & cmd connections, trying to combine the SQL queries into one executed script, opening one connection and trying to make the it equivalent to the other and execute that.

Try executing the scripts separately, opening and closing the connections separately (if you need separate connections). I suggest having another go and posting the result.
Dec 12 '06 #2

Thanks for the help. I changed my code drastically on your recommendations and came up with the following that actually worked.

Private Sub AddProduct()

Dim cnSQL = New SqlClient.SqlConnection
cnSQL.ConnectionString = "Data Source=[ServerName]; Initial Catalog=SenTest; password=""[Password]""; persist security info =True; user id=sa; workstation ID=[computerName]; Packet Size =4096"
Dim cmSQL As SqlClient.SqlCommand
Dim strSQL As String


' Build Insert statement to insert new product into the Bill table
strSQL = "Insert Bill Values (" & TextBox1.Text & "," & TextBox2.Text & " ," & TextBox3.Text & ")"

cmSQL = New SqlClient.SqlCommand(strSQL, cnSQL)

' Close and Clean up objects

Catch Exp As SqlClient.SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try


End Sub

And then in the end I just called the AddProduct sub on my button_click.

I am really new to this, and was struggling with it for a while now and you pointed me in the right direction.

Thank you very much!!!
Dec 12 '06 #3
I wonder if you could help me again. I am trying to get in a bit deeper and I am trying to swop TextBox3 with a combobox.

I am getting a new error that is saying: "Converting Varchar value 'ID' to a cloumn of data type int".

I am sure it is because I am not converting the value to int, but I have no idea how to do that.

Dec 12 '06 #4
4,871 Expert 4TB
Glad that it worked :). For the latest I would try using the ComboBox.SelectedText property, rather than attempting to convert an integer value.

Hope that this helps.
Dec 13 '06 #5
Dear ,

Can u please explain this statment to me .... am new .. excuse me .


Thanx very much
Feb 6 '07 #6

I ran into this a couple of times and being new to this myself my answer could be very limited. Here goes:

I am using Visual Studio 2005 and there is a lot of differences in this version and Visual Studio. The line that you are talking about is to fill a table adapter in Visual studio 2005. If that is the same as Visual studio am cannot say and I appologize, but I am also new.

Example: you have a datagrid on your application that is connected to a SQL database and you need to "get" your information from your Dataset into your datagrid, you will be using that line in either your button_click command or on your page load command to load the information into the datagrid.

The data Adapter also needs to be configured before it will work though. This I found very easy in Visual Studio 2005.

Hope this helps
Feb 8 '07 #7
Dear ,

Can u please explain this statment to me .... am new .. excuse me .


Thanx very much
If you mean explain what it is/means? Well, the TableAdapter is an object that processes SQL queries -- among other things -- and in this case, you're asking it to fill the "Bill" table in the"SenTest" dataset with the results of the SQL query deinfed for the Bill tableadapter -- probably something you set up when you first defined the data source, such as "Select * from Bills" or some such.

The table adapter most often is used to execute selections (from the host database, into your local working dataset), or updates (to the host database, to reflect changes you've made to your local dataset.)

I sure hope that's what you were asking.
Dec 19 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

by: GrantS | last post by:
I am unable to get the connection to work with using the app.config file. the connection works when I use 'in line' connection as below:...
by: MattB | last post by:
I'm trying to implement an example I found for displaying images stored in a SQL database. The example code looks like this (in page_load): Dim connstr As String = "Integrated...
by: Taishi | last post by:
ExecuteReader: Connection property has not been initialized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more...
by: Jason Williard | last post by:
I am trying to create a web form that will be used to create new users. The first step that I am taking is creating a web form that can check the username against a database to see if it already...
by: phil | last post by:
Hi, With the code below, i get the error: ExecuteReader: Connection property has not been initialized. Description: An unhandled exception occurred during the execution of the current web...
by: nada111 | last post by:
SelectCommand.Connection property has not been initialized
by: gggram2000 | last post by:
Hi, I'm using visual studio 2005 with sql server 2005. I made a program on my computer that works great. I wanted to transfer the same project through a remote connection to another computer, I...
by: santiago8000 | last post by:
hello,i'm new to vb.net,and i'm trying to do my first program for my training...my program is about the Northwind2 database in sql server,i have a "ShowAll" button,that displays in a form the...
by: phyton2 | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.