vb.net - I need help putting all the logic statements in the correct order -


when click search button, happen:

if search field empty, display message saying "please enter student number". if search field isn't empty, search database specific record matching user typed search field making sure there numbers in search field, if letter found, display message "numbers only", search db specific record. if record isn't found, display message saying "student not exist". if record found, populate fields data , based off of in record, enable buttons , change color.

that's want. , work...mostly. out of order or in wrong spot or something. can't figure out. catch statement towards bottom commented out because use work doesn't - again, or things out of order. i'm not advanced developer @ all, go easy on me.

here code click event of search button:

if stunumtxtbox.text = ""         msgbox("please enter student number.", msgboxstyle.exclamation)         stunumtxtbox.select()     else         try             using connection new sqlconnection("data source=server-name\iawdb;initial catalog=iawdb;persist security info=true;integrated security=true")                 connection.open()                 dim dt new datatable                 dim ds new dataset                 dim da new sqldataadapter                 ds.tables.add(dt)                 da = new sqldataadapter("select distinct * student_info studentid = '" & stunumtxtbox.text & "'", connection)                 dim count = da.fill(dt)                 if count = 0                     msgbox("student id not found.", msgboxstyle.critical)                 else                     dim g1 list(of string) = (from r datarow in dt r.field(of string)("g1") = "y" or r.field(of string)("g1") = "n" select r.field(of string)("g1")).tolist()                     me.g1button.backcolor = if(g1.contains("y"), color.green, if(g1.contains("n"), color.red, systemcolors.controldarkdark))                     me.g1button.enabled = (g1.count > 0)                      dim g2 list(of string) = (from r datarow in dt r.field(of string)("g2") = "y" or r.field(of string)("g2") = "n" select r.field(of string)("g2")).tolist()                     me.g2button.backcolor = if(g2.contains("y"), color.green, if(g2.contains("n"), color.red, systemcolors.controldarkdark))                     me.g2button.enabled = (g2.count > 0)                      dim af1 list(of string) = (from r datarow in dt r.field(of string)("af1") = "y" or r.field(of string)("af1") = "n" select r.field(of string)("af1")).tolist()                     me.af1button.backcolor = if(af1.contains("y"), color.green, if(af1.contains("n"), color.red, systemcolors.controldarkdark))                     me.af1button.enabled = (af1.count > 0)                      dim af2 list(of string) = (from r datarow in dt r.field(of string)("af2") = "y" or r.field(of string)("af2") = "n" select r.field(of string)("af2")).tolist()                     me.af2button.backcolor = if(af2.contains("y"), color.green, if(af2.contains("n"), color.red, systemcolors.controldarkdark))                     me.af2button.enabled = (af2.count > 0)                      dim af3 list(of string) = (from r datarow in dt r.field(of string)("af3") = "y" or r.field(of string)("af3") = "n" select r.field(of string)("af3")).tolist()                     me.af3button.backcolor = if(af3.contains("y"), color.green, if(af3.contains("n"), color.red, systemcolors.controldarkdark))                     me.af3button.enabled = (af3.count > 0)                      dim pp1 list(of string) = (from r datarow in dt r.field(of string)("pp1") = "y" or r.field(of string)("pp1") = "n" select r.field(of string)("pp1")).tolist()                     me.pp1button.backcolor = if(pp1.contains("y"), color.green, if(pp1.contains("n"), color.red, systemcolors.controldarkdark))                     me.pp1button.enabled = (pp1.count > 0)                      dim pp2 list(of string) = (from r datarow in dt r.field(of string)("pp2") = "y" or r.field(of string)("pp2") = "n" select r.field(of string)("pp2")).tolist()                     me.pp2button.backcolor = if(pp2.contains("y"), color.green, if(pp2.contains("n"), color.red, systemcolors.controldarkdark))                     me.pp2button.enabled = (pp2.count > 0)                      dim pp3 list(of string) = (from r datarow in dt r.field(of string)("pp3") = "y" or r.field(of string)("pp3") = "n" select r.field(of string)("pp3")).tolist()                     me.pp3button.backcolor = if(pp3.contains("y"), color.green, if(pp3.contains("n"), color.red, systemcolors.controldarkdark))                     me.pp3button.enabled = (pp3.count > 0)                      editstudenttoolstripmenuitem.enabled = true                 end if                 each datarow in dt.rows                     if stunumtxtbox.text = dt.rows(0)("studentid").tostring                         fnametxtbox.text = dt.rows(0)("firstname").tostring                         mnametxtbox.text = dt.rows(0)("midleinitial").tostring                         lnametxtbox.text = dt.rows(0)("lastname").tostring                         addresstxtbox.text = dt.rows(0)("addressstreet").tostring                         address2txtbox.text = dt.rows(0)("addressoption").tostring                         citytxtbox.text = dt.rows(0)("addresscity").tostring                         statetxtbox.text = dt.rows(0)("addressstate").tostring                         ziptxtbox.text = dt.rows(0)("addresszip").tostring                         countrytxtbox.text = dt.rows(0)("addresscountry").tostring                         celltxtbox.text = dt.rows(0)("contactcellphone").tostring                         studentidtxtbox.text = dt.rows(0)("studentid").tostring                         timeowedtxtbox.text = dt.rows(0)("timeowed").tostring                         studentnametextbox.text = lnametxtbox.text & "," & " " & fnametxtbox.text & " " & mnametxtbox.text                     else                     end if                 next             end using         catch se sqlexception             'if se.errorcode = -2146232060             'msgbox("enter numbers only.", msgboxstyle.information)             msgbox(se.message)             'else             msgbox(se.message)             'end if                 end try     end if 

is there out there may tadddddddd bit better @ me, flip around me , make work , me understand changes made , why made? haha know, asking lot. i'm close though...i think.

well, first thing need test if stunumtxtbox.text contains value can parsed integer. easy way use integer.tryparse method. use right after test empty string. also, test empty string can better. use string.isnullorwhitespace method. see code example:

if not string.isnullorwhitespace(stunumtxtbox.text) dim integer     if integer.tryparse(stunumtxtbox.text, i)         '' string contains valid integer, can continue code here.     else         msgbox("enter numbers only.", msgboxstyle.information)         stunumtxtbox.select()     end if else     msgbox("please enter student number.", msgboxstyle.exclamation)     stunumtxtbox.select() end if 

as rule, exceptions should not used input validations. never use try...catch things can test in other simple ways.

one last thing: loop redundant. if have 1 row, there no need loop.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -