Unable to make Connection to SQL Database

ThomasM

Member
Joined
Apr 5, 2006
Messages
17
Programming Experience
Beginner
I posted earlier and have made some progress. But Was wondering if someone could PLEASE tell me what I am doing wrong or proint me in the right direction.

Many thanks!!!!

I keep getting the following message:
Object reference not set to an instance of an object.

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif]Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 445:Line 446: Dim CPhoneNum, CWorkNum, CCellNum As StringLine 447: myConnection.ConnectionString = connStringLine 448: myConnection.Open()Line 449:[/FONT]
Here is my Code:
Protected Sub bCreateCust_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCreateCust.Click
Dim ra As Integer
Dim myConnection As SqlConnection = Nothing
Dim myCommand As SqlCommand = Nothing
Dim connString As String = "Integrated Security=SSPI;" + "Initial Catalog=CustomerInfo;" + "Data Source=localhost;"

Dim CPhoneNum, CWorkNum, CCellNum As String
myConnection.ConnectionString = connString
myConnection.Open()
Const sql As String = "Insert into CustomerTable (WorkSheetNum, DealerShipName, SalesRep1, Bkupslsrep, FirstName, MiddleName, Lastname, Address, City, State, Zip, HomeNum, WorkNum, CellNum, Email, County, SalesTax) Values (@WorkSheetNum, @DealerShipName, @SalesRep1, @Bkupslsrep, @FirstName, @MiddleName, @Lastname, @Address, @City, @State, @Zip, @HomeNum, @WorkNum, @CellNum, @Email, @County, @StaxRate)"
CPhoneNum = ddlHPAC.Text + tbhomephone.Text
CWorkNum = ddlwpAC.Text + tbWorkPhone.Text
CCellNum = ddlCPAC.Text + tbCellPhone.Text

With myCommand.Parameters
.Add(
New SqlParameter("@WorkSheetNum", tbWorkSheetNum.Text))
.Add(
New SqlParameter("@DealerShipName", ddlDealerShipName.Text))
.Add(
New SqlParameter("@SalesRep1", "TMORITZ"))
.Add(
New SqlParameter("@Bkupslsrep", ddlBkupSlsRep.Text))
.Add(
New SqlParameter("@FirstName", tbFirstName.Text))
.Add(
New SqlParameter("@MiddleName", tbMiddleName.Text))
.Add(
New SqlParameter("@Lastname", tblastname.Text))
.Add(
New SqlParameter("@Address", tbBuyAddress.Text))
.Add(
New SqlParameter("@City", tbcity.Text))
.Add(
New SqlParameter("@State", ddlState.Text))
.Add(
New SqlParameter("@Zip", tbZip.Text))
.Add(
New SqlParameter("@HomeNum", CPhoneNum))
.Add(
New SqlParameter("@WorkNum", CWorkNum))
.Add(
New SqlParameter("@CellNum", CCellNum))
.Add(
New SqlParameter("@Email", tbEmailAddress.Text))
.Add(
New SqlParameter("@County", tbBuyersCounty.Text))
.Add(
New SqlParameter("@StaxRate", ddlSalesTax.Text))
End With

myCommand = New SqlCommand(sql, myConnection)
ra = myCommand.ExecuteNonQuery()
Response.Write(
"Records Inserted" & ra)
myConnection.Close()
End Sub
Thank you!!!
TJ
 
You've dimmed myConnection As SqlConnection and set it to nothing.... so it doesn't exist when you set the connection string. You need to first instanciate the object before you can use it.

-tg
 
Back
Top