user3762349
user3762349

Reputation: 1

Error in oledb database connectivity

Connection

Conn_String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\UTS.mdb"
conn = New OleDbConnection(Conn_String)
conn.Open()

Query

sqlCmd.Connection = conn

sqlCmd.CommandText = "INSERT into Customer_Master Values (@Cust_ID,@Cust_Name,@Cust_Address,@Cust_ContactNo)"

sqlCmd.Parameters.AddWithValue("@Cust_ID", SqlDbType.Int).Value = Cust_id
sqlCmd.Parameters.AddWithValue("@Cust_Name", SqlDbType.Text).Value = txtcname.Text
sqlCmd.Parameters.AddWithValue("@Cust_Address", SqlDbType.Text).Value = txtcadd.Text
sqlCmd.Parameters.AddWithValue("@Cust_ContactNo", SqlDbType.Int).Value = 
txtccontact.Text.ToString

sqlCmd.ExecuteNonQuery()
conn.Close()

Problem

When I Click the button containing the above code it gives me following error..

An unhandled exception of type System.NullReferenceException occurred in UTS.exe

Additional information: Object reference not set to an instance of an object.

The error is n this line-->sqlCmd.Connection = conn

Upvotes: 0

Views: 131

Answers (1)

Nick.Mc
Nick.Mc

Reputation: 19184

When I searched in ExcuteNonQuery to check myself, I found this example first:

msdn

it shows an example of intialising a command object:

SqlCommand command = new SqlCommand(queryString, connection);

Upvotes: 1

Related Questions