sql - Detecting if data exists for OleDB COUNT query -


i'm trying pull data access database.

as is, code works, , gives no errors... however, can't seem able display messagebox if record doesn't exist. returns empty string.

        using dbcon = new oledbconnection("provider=microsoft.ace.oledb.12.0;" & "data source = '" & application.startuppath & "\res\t500g.accdb'")          dbcon.open()          dim query1 string = "select sum(total) [t500] pro=@pro"          dim cmd1 oledb.oledbcommand = new oledbcommand(query1, dbcon)         cmd1.parameters.addwithvalue("@pro", comboboxbp.selecteditem.tostring)          dim reader oledb.oledbdatareader         reader = cmd1.executereader           while reader.read()              textbox1.text = reader.getvalue(0).tostring          end while          reader.close()         dbcon.close()      end using 

i've tried using if reader.hasrows then display result in textbox, else show messagebox etc, doesn't work.

        if reader.hasrows             while reader.read()                  textbox1.text = reader.getvalue(0).tostring              end while         else              messagebox.show("asd")          end if 

if remove .tostring reader.getvalue(0) error if selected item combobox doesn't exist in database. cannot convert dbnull integer or something.

so question is, how display messagebox if record "@pro" doesn't exist?

thanks~

fixed(workaround) this

dim valuereturned string         while reader.read()              valuereturned = reader.getvalue(0).tostring              if valuereturned = ""                  messagebox.show("not found", "error", messageboxbuttons.ok, messageboxicon.error)              else                  metrotextbox1.text = valuereturned              end if          end while 

using oledbdatareader suboptimal query because never going return set of records traverse:

dim sql = "select sum(...=@p1"  ' rather sprinkling connection string on ' app, can make function returning 1 using conn oledbconnection = getconnection(),         cmd new oledbcommand(sql, getconnection())     conn.open())      ' todo: check comboboxbp.selecteditems.count >0 before     cmd.parameters.addwithvalue("@p1", comboboxbp.selecteditem.tostring)      ' execute query, result     dim total = cmd.executescalar      ' if there no matches, oledb returns dbnull     if total system.dbnull.value         ' no matches         messagebox.show("no matching records!"...)     else         messagebox.show("the total is: " & total.tostring()...)     end if  end using   ' disposes of connection , command objects 

alteratively, use if isdbnull(total) then.... if want, can convert it:

dim total = cmd.executescalar.tostring() ' dbnull convert empty string if string.isnullorempty(total)     messagebox.show("no soup you!") else     ... end if 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -