vb.net - I m trying to fetch total in to TextBox5.text but error occured -
i m trying fetch total in textbox5.text error occured no value given 1 or more required parameters.
try con.close()
con.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=pradnya_datab.mdb" con.open() ss = "select total stock_bottle bottle_type=" & convert.tostring(textbox3.text.tostring()) cmd = new oledbcommand(ss, con) rd = cmd.executereader() if rd.read() textbox5.text = rd("total").tostring() end if con.close() rd.close() catch ex exception msgbox(ex.message) end try
this error occurs when 1 or more of column names , table name not spelled correctly. in case, means total
or bottle_type
or stock_bottle
not correct name fields or table.
saying that, , pending due correction query, suggest use more robust code one
try dim ss = "select total stock_bottle bottle_type=@btype" using con = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=pradnya_datab.mdb") using cmd = new oledbcommand(ss, con) con.open() cmd.parameters.add("@btype", oledbtype.int32).value = textbox3.text dim result = cmd.executescalar() if result isnot nothing textbox5.text = result.tostring() end if end using end using catch ex exception msgbox(ex.message) end try
here have changed query parameterized query. it more safe , robust correct interpretation of datatype used clause. have removed global variable connection. (you don't advantage , need check status of connection).
have added using statement ensure proper closing of connection , immediate release of system resources kept connection.
finally, if need retrieve 1 row 1 single column not use more expensive oledbdatareader
(useful when there many rows , columns read back) directly oledbcommand executescalar method
Comments
Post a Comment