Example of some methods in asp.net
Example of various methods in asp.net c# which we need always.
*HTML:
<div class="container">
<div class="col-md-4">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="table">
<thead>
<h3>Update Customer </h3>
</thead>
<tr>
<td>First Name </td>
<td> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Last Name </td>
<td> <asp:TextBox ID="txtLast" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Email </td>
<td>
<asp:TextBox ID="txtEmail" runat="server" type="email"></asp:TextBox>
</td>
</tr>
<tr>
<td>Phone</td>
<td>
<asp:TextBox ID="txtPhone" runat="server" type="number"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="90px" OnClick="Button1_OnClick" />
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</td>
</tr>
<tr>
<p>
<asp:Label runat="server" ID="lblmsg"></asp:Label>
</p>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="col-md-4">
<br />
<h2>Gridview Row Delete</h2>
<asp:GridView runat="server" ID="GridView1" DataKeyNames="id" class="table table-bordered" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<a href="inserCustomer.aspx?id=<%#Eval("id") %>">Edit</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="deleteButton" CssClass="btn btn-danger" runat="server" CommandName="Delete" Text="X" OnClientClick="return confirm('Are you sure you want to delete this Information?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
1. Inline: private readonly SqlConnection con =
new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=AjaxTestProject;Integrated Security=True;");
2. In web config: <add name="ConStr" connectionString="Data Source=.\sqlexpress;Initial Catalog=AjaxTestProject;Integrated Security=True;"
providerName="System.Data.SqlClient" />
This is Our Table:
1. Save data using parameter wise insert procedure:
protected void Button1_OnClick(object sender, EventArgs e)
{
name = txtName.Text;
last = txtLast.Text;
email = txtEmail.Text;
phone = txtPhone.Text;
var cmd = new SqlCommand("sp_insert_tblCustomer", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@last", last);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@phone", phone);
con.Open();
int m = cmd.ExecuteNonQuery();
if (m != 0)
{
lblmsg.Text = "Record Inserted Succesfully";
lblmsg.ForeColor = Color.Green;
}
con.Close();
}
2. Show data using without parameter or stored procedure:
private void GetCustomer()
{
var dt = new DataTable();
var da = new SqlDataAdapter("SELECT * from tblCustomer", con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
3.1 Show data using parameter but not using stored procedure:
private void ShowDataIdWise()
{
string Edit_Id = Request.QueryString["id"];
var dt = new DataTable();
var da = new SqlDataAdapter("SELECT * from tblCustomer where id=" + Edit_Id, con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
DataRow row = dt.Rows[0];
txtName.Text = Convert.ToString(row["FirstName"]);
txtLast.Text = Convert.ToString(row["LastName"]);
txtEmail.Text = Convert.ToString(row["email"]);
txtPhone.Text = Convert.ToString(row["phone"]);
}
}
3.2 Show data using parameter wise stored procedure:
private void ShowDataIdWise()
{
string Edit_Id = Request.QueryString["id"];
DataTable dt2 = new DataTable();
SqlCommand cmd = new SqlCommand("sp_tblCustomer_id_wise", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", Edit_Id);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt2);
con.Close();
if (dt2.Rows.Count > 0)
{
DataRow row = dt2.Rows[0];
txtName.Text = Convert.ToString(row["FirstName"]);
txtLast.Text = Convert.ToString(row["LastName"]);
txtEmail.Text = Convert.ToString(row["email"]);
txtPhone.Text = Convert.ToString(row["phone"]);
}
}
4. Update data using parameter wise stored procedure:
protected void btnUpdate_Click(object sender, EventArgs e)
{
string Edit_Id = Request.QueryString["id"];
name = txtName.Text;
last = txtLast.Text;
email = txtEmail.Text;
phone = txtPhone.Text;
var cmd = new SqlCommand("sp_update_tblCustomer", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", Edit_Id);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@last", last);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@phone", phone);
con.Open();
int m = cmd.ExecuteNonQuery();
if (m != 0)
{
lblmsg.Text = "Data Updated Succesfully";
lblmsg.ForeColor = Color.Green;
}
con.Close();
}
5. Delete data using GridView built in OnRowDeleting method, not using stored procedure:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("delete from tblCustomer where id=" + Id, con);
int result = cmd.ExecuteNonQuery();
con.Close();
GetCustomer();
}
Thank you for sharing your thoughts and knowledge on this topic. This is really helpful and informative, as this gave me more insight to create more ideas and solutions for my plan. I would love to see more updates from you.
ReplyDeleteMelbourne Web Developer