mysql - The connection property has not been set or is null -
when run function
repeatbooking = 1 51 dateconverteddatetobook = datedatetobook.date datedatetobook = datedatetobook.adddays(7) strdatetobook = dateconverteddatetobook.tostring("yyyy-mm-dd") seek dim command mysqlcommand = new mysqlcommand dim sqlquery string = "insert bookings set date=" & "'" & strdatetobook & "',roomid='" & strcomputerroomtobook & "',length='" & intnewbookinglength & "',period='" & intnewstartperiod & "',userid='" & intid & "'" dim reader mysqldatareader sqlconnection.open() command.commandtext = sqlquery command.connection = sqlconnection reader = command.executereader sqlconnection.close() grab excep exception msgbox(excep.tostring) end seek next in programme error saying "the connection property has not been set or null" how can rid of this?
it goes exception when gets sqlconnection.open() created serverstring , mysql connection @ top of module so:
dim serverstring string = "server=localhost;user id=root;password=**********;database=rooms" dim sqlconnection mysqlconnection = new mysqlconnection
you opening connection without property should be,
dim sqlconnection new mysqlconnection(serverstring) sqlconnection.open also, may want utilize using function connection closed. seems inserting bunch of values database , not retrieving why utilize datareader? code should this:
using sqlconnection = new mysqlconnection(serverstring) sqlconnection.open 'you should open connection 1 time repeatbooking = 1 51 dateconverteddatetobook = datedatetobook.date datedatetobook = datedatetobook.adddays(7) strdatetobook = dateconverteddatetobook.tostring("yyyy-mm-dd") seek dim sqlquery string = "insert bookings set " & _ "date='" & strdatetobook & "'," & _ "roomid='" & strcomputerroomtobook & "', " & _ "length='" & intnewbookinglength & "', " & _ "period='" & intnewstartperiod & "', " & _ "userid='" & intid & "'" dim command = new mysqlcommand(sqlquery, sqlconnection) command.executenonquery grab excep exception msgbox(excep.message) end seek next end using also, may want alter how pass values parameter. prevent sql injection.
mysql vb.net exception date command
No comments:
Post a Comment