c# - Exits don't insert else insert -
i have started larn asp.net. using vs 2013 express c#. how create if case check duplicate value , if value exists reddish summary , can't insert db else insert database , update button.
can help?
sqlconnection con = new sqlconnection(@"data source=tss\sqlexpress;initial catalog=db;integrated security=true"); protected void add(object sender, eventargs e) { var vardas = gridview1.footerrow.findcontrol("txtname") textbox; var pavarde = gridview1.footerrow.findcontrol("txtlastname") textbox; var pozymis = gridview1.footerrow.findcontrol("dropdownlist2") dropdownlist; sqlcommand comm = new sqlcommand(); comm.commandtext = "insert asmenys (name,lastname, status) values(@name,@lastname, @status)"; comm.connection = con; comm.parameters.addwithvalue("@name", name.text); comm.parameters.addwithvalue("@lastname", lastname.text); comm.parameters.addwithvalue("@status", status![enter image description here][1].text); con.open(); comm.executenonquery(); con.close(); databind(); }
when check if value exists, fields should not have duplicates? fields have write select statement check if exist first.
example
protected void add(object sender, eventargs e) { var vardas = gridview1.footerrow.findcontrol("txtname") textbox; var pavarde = gridview1.footerrow.findcontrol("txtlastname") textbox; var pozymis = gridview1.footerrow.findcontrol("dropdownlist2") dropdownlist; sqlcommand comm = new sqlcommand(); comm.commandtext = "select lastname asmenys lastname = @lastname"; comm.parameters.addwithvalue("@lastname", lastname.text); sqldatareader reader = comm.executereader(); if (reader.hasrows) { console.writeline("values exist"); } else { comm.commandtext = "insert asmenys (name,lastname, status) values(@name,@lastname, @status)"; comm.connection = con; comm.parameters.addwithvalue("@name", name.text); comm.parameters.addwithvalue("@lastname", lastname.text); comm.parameters.addwithvalue("@status", status![enter image description here][1].text); con.open(); comm.executenonquery(); con.close(); databind(); } }
c# asp.net ado.net
No comments:
Post a Comment