sql server - I try to update to SQL but it show this error " object reference is not set instance of an object " -


private sub button1_click(sender object, e eventargs) handles button1.click     dim con sqlconnection      con = new sqlconnection("data source=.;initial catalog=pantiendatabase;integrated security=true")     con.open()     try         'cmd.commandtext = "update patient_detail set (name ='" & textboxname.text & "',age = '" & textboxage.text & "',sex = '" & textboxsex.text & "',address = '" & textboxaddress.text & "',check_in = '" & textboxchin.text & "',check_in_illness = '" & textboxchinl.text & "',sevice = '" & textboxservice.text & "',check_out_illness = '" & textboxchoutl.text & "',check_out = '" & textboxchout.text & "',transfer = '" & textboxtransfer.text & "',patient_result = '" & comboboxptr.text & "') id = '" & textboxid.text & "' "         cmd.commandtext = "update patient_detail set (name = @name, age = @age, sex = @sex, address = @address, check_in = @check_in, check_in_illness =@check_in_illness, sevice =@service, check_out_illness =@check_out_illness, check_out = @check_out ,transfer = @transfer, patient_result = @pantient_result id = @id "          cmd.parameters.addwithvalue("@id", textboxid.text)         cmd.parameters.addwithvalue("@name", textboxname.text)         cmd.parameters.addwithvalue("@age", textboxage.text)         cmd.parameters.addwithvalue("@sex", textboxsex.text)         cmd.parameters.addwithvalue("@address", textboxaddress.text)         cmd.parameters.addwithvalue("@check_in", textboxchin.text)         cmd.parameters.addwithvalue("@check_in_illness", textboxchinl.text)         cmd.parameters.addwithvalue("@service", textboxservice.text)         cmd.parameters.addwithvalue("@check_out_illness", textboxchoutl.text)         cmd.parameters.addwithvalue("@check_out", textboxchout.text)         cmd.parameters.addwithvalue("@transfer", textboxtransfer.text)         cmd.parameters.addwithvalue("@pantient_result", comboboxptr.text)          cmd.executenonquery()     catch ex exception         messagebox.show(ex.message)     end try     con.close() 

you don't declare cmd anywhere. @ least, not in code block. thus, cmd not set instance of anything.

add under declaration of con:

dim cmd sqlcommand

the set new instance , set con connection @ top of try block:

cmd = new sqlcommand() cmd.connection = con 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -