Asp.Net Tutorials: Reading in, manipulating, and then putting it back into SQL server



Try to use template field instead of bound field in this way:
<asp:GridView ID="gvDetails" runat="server" DataKeyNames="PKId" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<label>
HomeTown</label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label Text='<%# Eval("HomeTown") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
 
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="Select" Text="Select" />
</Columns>
</asp:GridView>

 <asp:DetailsView ID="DetailsView1" runat="server" DataKeyNames="PKId" AutoGenerateRows="False"
 OnItemCommand="DetailsView1_ItemCommand"
 >
 <Fields>
 <asp:TemplateField>
 <HeaderTemplate>
  <label> HomeTown</label>
 
</HeaderTemplate>
<InsertItemTemplate><asp:TextBox ID="txt HomeTown Insert" Text='<%# Bind("HomeTown") %>' runat="server" />
</InsertItemTemplate>
<ItemTemplate>
 <asp:Label ID="lblHomeTown" runat="server" Text='<%# Eval("HomeTown ") %>' />
 </ItemTemplate>
 <EditItemTemplate>
<asp:TextBox ID="txtHomeTown" Text='<%# Bind("HomeTown") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
                        
 <asp:TemplateField ShowHeader="False">
 <ItemTemplate>
<asp:Button runat="server" CausesValidation="true" CommandName="Edit" Text="Edit" />
 <asp:Button ID="btnCancelEdit" runat="server" CommandName="Cancel" Text="Cancel" />
</ItemTemplate>
<InsertItemTemplate>
<asp:Button ID="btnAdd" runat="server" CommandName="Add" Text="Add" />
<asp:Button ID="btnCancelAdd" runat="server" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField ShowHeader="False">
 <EditItemTemplate>
 </EditItemTemplate>
 </asp:TemplateField>
 </Fields>
</asp:DetailsView>


Code Behind File:
protected void gvDetails_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
gvDetails.Enabled = false;
}

    private void BindDetailsView()
    {
 
        if (gvDetails.SelectedValue != null)
        {
          int indRow = Convert.ToInt32(gvDetails.SelectedValue);
          DetailsView1.DataSource = new LoginBO().GetSingleById(indRow);
            DetailsView1.DataBind();
        }
 
    }
    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Edit"))
        {
 
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            BindDetailsView();
        }
        if (e.CommandName == "Update")
        {
 
            int pkID = Convert.ToInt32(gvDetails.SelectedValue);
            Login l = new Login();
            l.PkLoginId = pkID;
            TextBox txt;
            txt = (TextBox)DetailsView1.FindControl("txtHomeTown");
            l.Name = txt.Text;
            
            new LoginBO().UpdateInfo(l);
            Response.Redirect(Request.RawUrl);
        }
        else if (e.CommandName == "Add")
        {
            Login l = new Login();
            TextBox txt;
            txt = (TextBox)DetailsView1.FindControl("txtHomeTownInsert");
            l.Name = txt.Text;
            
            new LoginBO().AddInfo(l);
            gvDetails.DataBind();
            Response.Redirect(Request.RawUrl);
 
        }
        else if (e.CommandName == "Cancel")
        {
            Response.Redirect(Request.RawUrl);
        }
    }

In LoginBO.cs add the following code:
DataClassesDataContext dc;
dc=new DataClassesDataContext ();
public IEnumerable<Login> GetSingleById(int? pkLoginId)
{
 
var qry2 = (from l in dc.Logins
where l.PkLoginId == pkLoginId
select l);
return qry2;
}
public void UpdateInfo(Login oldLogin)
{
Login newLogin = new Login();
newLogin = dc.Logins.Where(e => e.PkLoginId == oldLogin.PkLoginId).SingleOrDefault();
if (newLogin != null)
{
newLogin.HomeTown = oldLogin.HomeTown;
}
try
{
dc.SubmitChanges();
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
public void AddInfo(Login oldLogin)
{
Login newLogin = new Login();
if (newLogin != null)
{
newLogin.HomeTown = oldLogin.HomeTown;
dc.Logins.InsertOnSubmit(newLogin);
try
{
dc.SubmitChanges();
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
}
For more information Click here

Comments