Asp.net c# SQL Server Update query not working for all columns -
i have form should edit information in database. first, query database using url parameter record , display them in textboxes update them. part of code works issue when press submit update data after changing what's on textboxes. lastmod
column gets updated. think page_load
fires twice , overwrites what's on textboxes before button click event fires.
here's code.
public partial class edit : system.web.ui.page { int aid; sqlconnection conn = new sqlconnection(@connectionstring); protected void page_load(object sender, eventargs e) { if (!ispostback) { conn.open(); aid = convert.toint16(request.querystring["id"]); string sql = "select * contacts id = '" + aid + "' "; sqldataadapter adap = new sqldataadapter(sql, conn); dataset ds = new dataset(); adap.fill(ds); txtfname.text = ds.tables[0].rows[0]["fname"].tostring(); txtlname.text = ds.tables[0].rows[0]["lname"].tostring(); txtemail.text = ds.tables[0].rows[0]["emailadd"].tostring(); lbllastmod.text = ds.tables[0].rows[0]["lastmod"].tostring(); } } protected void button1_click(object sender, eventargs e) { datetime date = datetime.now; sqlcommand sqlcomm = new sqlcommand(); sqlcomm = conn.createcommand(); sqlcomm.commandtext = @"update contacts set fname=@fname, lname = @lname, emailadd = @eadd,lastmod = @lmod id=@id"; sqlcomm.parameters.add("@fname", sqldbtype.nchar); sqlcomm.parameters["@fname"].value = txtfname.text.trim(); sqlcomm.parameters.add("@lname", sqldbtype.nchar); sqlcomm.parameters["@lname"].value = txtlname.text.trim(); sqlcomm.parameters.add("@eadd", sqldbtype.nchar); sqlcomm.parameters["@eadd"].value = txtemail.text.trim(); sqlcomm.parameters.add("@lmod", sqldbtype.datetime); sqlcomm.parameters["@lmod"].value = date; sqlcomm.parameters.add("@id", sqldbtype.int); sqlcomm.parameters["@id"].value = aid; conn.open(); sqlcomm.executenonquery(); conn.close(); } }
i tried adding !ispostback
in page_load
method nothing gets updated lastmod
record.
thanks get.
you declare variable, aid, @ top of page, , set value in in page load. use in click, never re-set it. therefore, postback, not have value. move page load above "if not ispostback"
protected void page_load(object sender, eventargs e) { aid = convert.toint16(request.querystring["id"]); if (!ispostback) { conn.open(); string sql = "select * contacts id = '" + aid + "' ";
Comments
Post a Comment