Grid View Data Edit Delete Insert

<div id="divAddAlumniInformation" runat="server">
<div align="center" style="font-size: 20px;">
Add Alumni Information
</div>
<div align="center" style="font-family: Tahoma; color: Red;">
<asp:Label ID="lblAlumniInfo" runat="server"></asp:Label></div>
<div align="center">
<table>
<tr>
<td align="left">
Roll
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtAlumniRoll" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
Name
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtAlumniName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
Session
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtSession" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
Department
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtDepartment" runat="server" Text="ME"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="3" align="right">
<div>
<asp:Button ID="btnAddAlumni" runat="server" Text="Add Alumni" OnClick="btnAddAlumni_Click" />
</div>
<div>
<asp:GridView ID="gvwAlumniList" runat="server" AutoGenerateColumns="False" BackColor="White"
AllowPaging="True" PageSize="20" BorderStyle="None" ForeColor="Black" Width="450px"
OnPageIndexChanging="gvwAlumniList_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="Sl.">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll">
<ItemTemplate>
<div>
<asp:TextBox ID="txtAlumniRoll" Enabled="false" SkinID="special" runat="server" Text='<% #Eval("Roll") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alumni Name">
<ItemTemplate>
<div>
<asp:TextBox ID="txtAlumniName" runat="server" Text='<% #Eval("Name") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Session">
<ItemTemplate>
<div>
<asp:TextBox ID="txtAlumniSession" SkinID="special" runat="server" Text='<% #Eval("Session") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<div>
<asp:TextBox ID="txtAlumniDepartment" SkinID="special" runat="server" Text='<% #Eval("DepartmentId") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button ID="btnEditAlum" SkinID="btnSmall" runat="server" Text="Edit" OnClick="btnEditAlum_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btnDeleteAlum" SkinID="btnSmall" runat="server" Text="Delete" OnClick="btnDeleteAlum_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
</div>
</div>

divAddAlumniInformation.Visible = true;
AlumniShow();

protected void lbAddAlumniInformation_Click(object sender, EventArgs e)
{
Response.Redirect("frmAdminHome.aspx?qsId=AddAlumniInfo");
}

protected void btnAddAlumni_Click(object sender, EventArgs e)
{
String Qs = " SELECT * FROM T_Alumni WHERE Roll='" + txtAlumniRoll.Text.Trim() + "' ";
if (ClsCommon.GetAdhocResult(Qs).Tables[0].Rows.Count > 0)
{
lblAlumniInfo.Text = "The Roll is already exist !";
}
else
{

SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SelfAssessmentConnStr"].ConnectionString);
SqlCommand com = new SqlCommand();
SqlTransaction tran;
db.Open();
tran = db.BeginTransaction();
com.CommandText = "INSERT INTO T_Alumni(Roll,Name,Session,DepartmentId) VALUES(@Roll,@Name,@Session,@DepartmentId) ";
com.Connection = db;
com.Transaction = tran;
com.Parameters.Add("@Roll", SqlDbType.VarChar);
com.Parameters.Add("@Name", SqlDbType.VarChar);
com.Parameters.Add("@Session", SqlDbType.VarChar);
com.Parameters.Add("@DepartmentId", SqlDbType.VarChar);
try
{
com.Parameters["@Roll"].Value = txtAlumniRoll.Text.Trim();
com.Parameters["@Name"].Value = txtAlumniName.Text;
com.Parameters["@Session"].Value = txtSession.Text.Trim();
com.Parameters["@DepartmentId"].Value = txtDepartment.Text.Trim();
com.ExecuteNonQuery();
tran.Commit();
lblAlumniInfo.Text = "Alumni Insertion Successful.";
}
catch (SqlException sqlex)
{
tran.Rollback();
lblAlumniInfo.Text = "Alumni Insertion Failed. Error: " + sqlex.Message;
}
finally
{
db.Close();
}
}
}

void AlumniShow()
{
String QsAlumni = " SELECT Roll,Name,Session,DepartmentId FROM T_Alumni ";
gvwAlumniList.DataSource = ClsCommon.GetAdhocResult(QsAlumni).Tables[0];
gvwAlumniList.DataBind();
}

protected void btnEditAlum_Click(object sender, EventArgs e)
{
GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
Int32 index = row.RowIndex;
String Roll = ((TextBox)gvwAlumniList.Rows[index].FindControl("txtAlumniRoll")).Text;
String Name = ((TextBox)gvwAlumniList.Rows[index].FindControl("txtAlumniName")).Text;
String Session = ((TextBox)gvwAlumniList.Rows[index].FindControl("txtAlumniSession")).Text;
String DepartmentId = ((TextBox)gvwAlumniList.Rows[index].FindControl("txtAlumniDepartment")).Text;

try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SelfAssessmentConnStr"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE T_Alumni SET Name=@Name,Session=@Session,DepartmentId=@DepartmentId WHERE Roll=@Roll", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Roll", Roll.Trim().ToString()));
cmd.Parameters.Add(new SqlParameter("@Name", Name.ToString()));
cmd.Parameters.Add(new SqlParameter("@Session", Session.Trim().ToString()));
cmd.Parameters.Add(new SqlParameter("@DepartmentId", DepartmentId.Trim().ToString()));
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblAlumniInfo.Text = "Alumni Information Edited Successfully";
}
}
catch (Exception ex) { }
AlumniShow();
}
protected void btnDeleteAlum_Click(object sender, EventArgs e)
{
GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
Int32 index = row.RowIndex;
String Roll = ((TextBox)gvwAlumniList.Rows[index].FindControl("txtAlumniRoll")).Text;

try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SelfAssessmentConnStr"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM T_Alumni WHERE Roll=@Roll", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@Roll", Roll.Trim().ToString()));
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblAlumniInfo.Text = "Alumni has been Deleted Successsfully from database";
}
}
catch (Exception ex) { }
AlumniShow();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s