Hi,
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
Me.BillTableAdapter.Fill(Me.SenTestDataSet.Bill)
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
Conn.open()
cmd.Connection = Conn
InsertQuery = cmd.ExecuteNonQuery
Conn.Close()
End Sub
7 33519
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.
kenobewan,
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
cnSQL.Open()
Try
' 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)
cmSQL.ExecuteNonQuery()
' Close and Clean up objects
cnSQL.Close()
cmSQL.Dispose()
cnSQL.Dispose()
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
Me.BillTableAdapter.Fill(Me.SenTestDataSet.Bill)
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!!!
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.
Thanks!!
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.
Dear ,
Can u please explain this statment to me .... am new .. excuse me .
Me.BillTableAdapter.Fill(Me.SenTestDataSet.Bill)
Thanx very much
IceTalks,
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
Dear ,
Can u please explain this statment to me .... am new .. excuse me .
Me.BillTableAdapter.Fill(Me.SenTestDataSet.Bill)
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.
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: 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: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |