GridView With ObjectDataSource /Insert,Update & Delete With ObjectDataSource
In this article I will show you how you can use the ObjectDataSource with the GridView control to do editing, updating, deleting and adding new records. There are several ways to perform these operations, I am using the simplest approach.
CREATE TABLE [dbo].[tb_Country](
[Country_Id] [int] IDENTITY(1,1) NOT NULL,
[Country_Name] [varchar](50) NULL
)
Now To Explain You Below Are Two Images Will Tell You Why For What Code Is Going
Download Source From Here
In this article I will show you how you can use the ObjectDataSource with the GridView control to do editing, updating, deleting and adding new records. There are several ways to perform these operations, I am using the simplest approach.
For That We Need Grid View Like Below
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
onrowcommand="GridView1_RowCommand" ShowFooter="True"
AutoGenerateColumns="False" onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" onrowupdated="GridView1_RowUpdated"
onrowediting="GridView1_RowEditing" DataKeyNames="Country_Id">
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="TextBoxAddName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="LabelID" runat="server" Text='<%# Bind("Country_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBoxUpdateName" runat="server" Text='<%# Bind("Country_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddCountry"
Text="Add Country" />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Country_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
DataSourceID="ObjectDataSource1"
onrowcommand="GridView1_RowCommand" ShowFooter="True"
AutoGenerateColumns="False" onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" onrowupdated="GridView1_RowUpdated"
onrowediting="GridView1_RowEditing" DataKeyNames="Country_Id">
<Columns>
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID="TextBoxAddName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="LabelID" runat="server" Text='<%# Bind("Country_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBoxUpdateName" runat="server" Text='<%# Bind("Country_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddCountry"
Text="Add Country" />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Country_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Object DataSource Like Below
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="select" InsertMethod="AddCountry" UpdateMethod="UpdateCountry" DeleteMethod="DeleteUser" TypeName="ControlPanel.selection">
</asp:ObjectDataSource>
SelectMethod="select" InsertMethod="AddCountry" UpdateMethod="UpdateCountry" DeleteMethod="DeleteUser" TypeName="ControlPanel.selection">
</asp:ObjectDataSource>
Three Methods In Class File(Class File Under App_Code)
public void AddCountry(string CountryName)
{
}
public void UpdateCountry(string update,int uniqueid)
{
}
public void DeleteUser(int id)
{
}
{
}
public void UpdateCountry(string update,int uniqueid)
{
}
public void DeleteUser(int id)
{
}
Which we will Further Fill By
public void AddCountry(string CountryName)
{
SqlCommand cmd = new SqlCommand("insert into tb_Country values(@CountryName)", con);
cmd.Parameters.AddWithValue("@CountryName", CountryName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void UpdateCountry(string update,int uniqueid)
{
SqlCommand cmd = new SqlCommand("update tb_Country set Country_Name=@CountryN where Country_Id=@CID", con);
cmd.Parameters.AddWithValue("@CountryN", update);
cmd.Parameters.AddWithValue("@CID", uniqueid);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void DeleteUser(int id)
{
SqlCommand cmd = new SqlCommand("Delete from tb_Country where Country_Id=@CID", con);
cmd.Parameters.AddWithValue("@CID", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
{
SqlCommand cmd = new SqlCommand("insert into tb_Country values(@CountryName)", con);
cmd.Parameters.AddWithValue("@CountryName", CountryName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void UpdateCountry(string update,int uniqueid)
{
SqlCommand cmd = new SqlCommand("update tb_Country set Country_Name=@CountryN where Country_Id=@CID", con);
cmd.Parameters.AddWithValue("@CountryN", update);
cmd.Parameters.AddWithValue("@CID", uniqueid);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public void DeleteUser(int id)
{
SqlCommand cmd = new SqlCommand("Delete from tb_Country where Country_Id=@CID", con);
cmd.Parameters.AddWithValue("@CID", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Here Is The Script Of The TABLE
CREATE TABLE [dbo].[tb_Country](
[Country_Id] [int] IDENTITY(1,1) NOT NULL,
[Country_Name] [varchar](50) NULL
)
Now To Explain You Below Are Two Images Will Tell You Why For What Code Is Going
Download Source From Here
No comments:
Post a Comment