asp.net - Selected a row in a grid view, now want to update that record with update button click -


i have been stuck on , tried few different things use update button update record selected grid view. have 2 columns in table id , name. select record , populates text box name.... works fine. need take same record , update name same text box after pulled text box using update button click event. have 2 other buttons work fine "add" , "delete" , add code here code:

this how have populated grid view on page load or when call method:

private void populatecompanylistgrid() //this populate grid table data on page load     {         ilist<company> companies;          using (var context = new imsdbcontext())         {             companies = context.companies.tolist();         }          grdvwcompanylist.datasource = companies;         grdvwcompanylist.databind();     } 

this how grid view set up:

<asp:gridview runat="server" id="grdvwcompanylist" onselectedindexchanged="selectgridrow" datakeynames="id, name" allowsorting="true" autogenerateselectbutton="true"></asp:gridview> 

this how put selected record in text box:

public void selectgridrow(object sender, eventargs e) //this populate textbo row selected gridview     {         gridviewrow name = grdvwcompanylist.selectedrow;          if (name != null)         {             var datakey = grdvwcompanylist.datakeys[name.rowindex];             if (datakey != null)                 txtcompanyname.text = (string)datakey["name"];           }     } 

this how adding records:

protected void btnadd_click(object sender, eventargs e) // method adds record database     {         if (btnadd.text == "add") // clears textbox , notification label , calls method change name of button if button says "add"         {             txtcompanyname.text = "";             lblcompanynamenotification.text = "";             buttonchangeaddtosave();         }         else if (btnadd.text == "save") // checks if button says "save" , compares textbox , database matching record          {             imsdbcontext context = new imsdbcontext();             company companycheck = context.companies.singleordefault(company => company.name == txtcompanyname.text);              if (companycheck != null) // displays notification if there matching record             {                 lblcompanynamenotification.text = "there company name.";             }             else if(txtcompanyname.text == null)             {                  lblcompanynamenotification.text = "please enter name of company";             }             else if (txtcompanyname.text != null) // write record database if no matching record in database             {                 company n = new company();                 n.name = txtcompanyname.text.tostring();                   context.companies.add(n);                 context.savechanges();                  txtcompanyname.text = "";                 lblcompanynamenotification.text = "";                  buttonchangesavetoadd();             }         }         populatecompanylistgrid(); // calls method repopulate gridview     } 

add hidden field in markup hold company id:

<asp:hiddenfield id="hdncompanyid" runat="server" ></asp:hiddenfield> 

in selectgridrow method populate hidden field company id:

public void selectgridrow(object sender, eventargs e) //this populate textbo row selected gridview {     gridviewrow name = grdvwcompanylist.selectedrow;      if (name != null)     {         var datakeys = grdvwcompanylist.datakeys[name.rowindex];         if (datakeys["name"] != null)             txtcompanyname.text = (string)datakeys["name"];         if (datakeys["id"] != null)             hdncompanyid.value = datakeys["id"].tostring();     } } 

in btnupdate_click method company id , update :

protected void btnupdate_click(object sender, eventargs e)  {     int companyid;     string companyname = txtcompanyname.text;     if(int.tryparse(hdncompanyid.value, out companyid)){          imsdbcontext context = new imsdbcontext();         company company = context.companies.singleordefault(company => company.id == companyid);          if (company != null && txtcompanyname.text != "")         {             company.name = companyname;             context.savechanges();          }         else         {             lblcompanynamenotification.text = "the  company not exist.";         }     }      populatecompanylistgrid(); // calls method repopulate gridview             } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -