vb.net - ExecuteNonQuery: Connection property has not been initialized VB -
getting error here not sure why not opening connection. hoping can me.
protected sub btn_submit_click(byval sender system.object, e system.eventargs) handles btn_submit.click dim sqlstr string dim con sqlconnection dim connectionstring string = "data source=db\test;initial catalog=orders;integrated security=true" dim cmdinsert new sqlcommand(sqlstr, con) sqlstr = "insert customers(firstname,lastname,email,phone,address,city,state,zip) values (@firstname,@lastname,@email,@phone,@address,@city,@state,@zip)" try using connection new sqlconnection(connectionstring) connection.open() cmdinsert.parameters.add("@firstname", data.sqldbtype.nvarchar).value = firstname.text() cmdinsert.parameters.add("@lastname", data.sqldbtype.nvarchar).value = lastname.text cmdinsert.parameters.add("@email", data.sqldbtype.nvarchar).value = email.text cmdinsert.parameters.add("@phone", data.sqldbtype.nchar).value = phone.text cmdinsert.parameters.add("@address", data.sqldbtype.nvarchar).value = address.text cmdinsert.parameters.add("@city", data.sqldbtype.nvarchar).value = city.text cmdinsert.parameters.add("@state", data.sqldbtype.nvarchar).value = state.text cmdinsert.parameters.add("@zip", data.sqldbtype.nchar).value = zip.text cmdinsert.executenonquery() connection.close() end using catch ex exception msgbox(ex.message) end try
you have wrong scope -- you're instantiating sqlcommand
before instantiating , opening actual sql connection you're trying use execute command.
i believe fix code (i moved insert call using
scope):
protected sub btn_submit_click(byval sender system.object, e system.eventargs) handles btn_submit.click dim sqlstr string dim connectionstring string = "data source=db\test;initial catalog=orders;integrated security=true" sqlstr = "insert customers(firstname,lastname,email,phone,address,city,state,zip) values (@firstname,@lastname,@email,@phone,@address,@city,@state,@zip)" try using connection new sqlconnection(connectionstring) connection.open() dim cmdinsert new sqlcommand(sqlstr, connection) <----- **** moved here, changed connection cmdinsert.parameters.add("@firstname", data.sqldbtype.nvarchar).value = firstname.text() cmdinsert.parameters.add("@lastname", data.sqldbtype.nvarchar).value = lastname.text cmdinsert.parameters.add("@email", data.sqldbtype.nvarchar).value = email.text cmdinsert.parameters.add("@phone", data.sqldbtype.nchar).value = phone.text cmdinsert.parameters.add("@address", data.sqldbtype.nvarchar).value = address.text cmdinsert.parameters.add("@city", data.sqldbtype.nvarchar).value = city.text cmdinsert.parameters.add("@state", data.sqldbtype.nvarchar).value = state.text cmdinsert.parameters.add("@zip", data.sqldbtype.nchar).value = zip.text cmdinsert.executenonquery() connection.close() end using catch ex exception msgbox(ex.message) end try
Comments
Post a Comment