Deletion of records in asp.net using C#

In this tutorial we will learn how to perform the deletion of records in asp.net using C#. First of all we will show all records coming from database in gridview and then using gridview’s built-in event RowDeleting we will perform deletion of records in asp.bet using C#. To perform deletion of records in aso.net using C# is quite easy.

deleting_records_in_gridview.aspx

<asp:gridview id="GridView1" runat="server" width="100%">

<Columns>

<asp:TemplateField HeaderText="ID" >
<ItemTemplate>
<asp:Label ID="lbl_UserID" Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Delete" >                       

<ItemTemplate>

<asp:Label ID="lbl_delete" Visible="false"  Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>                        

<asp:ImageButton runat="server" ID="img" ImageUrl="images/b_drop.png" CommandName="Delete"  />

</ItemTemplate>                        

</asp:TemplateField>
</Columns>
</asp:gridview>

 

deleting_records_in_gridview.aspx.cs

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;


public partial class Tutorials_deleting_data_in_gridview : System.Web.UI.Page
{
string strcon = "server=localhost;database=tutorials-db; uid=adeel;password=pakistan; Connect Timeout=200; Max Pool Size=5000"; 


SqlConnection con = new SqlConnection(); 

SqlCommand com = new SqlCommand(); 

DataSet ds = new DataSet(); 

SqlDataAdapter da; 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
} 


private void bindgrid()
{
ds.Clear();
con.ConnectionString = strcon; 


com.Connection = con;

con.Open();

string strQuery = "select * from users"; 

da = new SqlDataAdapter(strQuery,con);

da.Fill(ds); 


GridView1.DataSource = ds;

GridView1.DataBind();

con.Close();
}

//Deleting records from database in asp.net using C#

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

  con.ConnectionString=strcon;

        com.Connection=con;

        con.Open();

        int index = e.RowIndex;        

        GridViewRow row=GridView1.Rows[index];

        Label lbl = (Label)row.FindControl("lbl_delete");

        string id = lbl.Text;

        string strDelete = "delete from users where User_ID='"+id+"'";

        com.CommandText = strDelete;

        com.ExecuteNonQuery();

con.Close();

        bindgrid();

  }

//Deleting records function ends here

}

So this is the way to perform deletion of data/records in asp.net using C#

Happy Coding, Keep Codig…!

 

 

Read more...

how to format phone number in ms sql server?

Sometimes it is desperately required to format the phone number before displaying them in your website. In this ms sql server tutorial we will learn how to format the phone number in mssql server query.

Actually we want to convert telephone number like this 6092758415 into (609) 275-8415. Let’s have a look over how to do this, for example we have a column user_phone in our database table users, let’s look how to format the telephone number in mssql server query. It's easy and better to perform formatting of phone numbers in sql.

 select case when len(ltrim(rtrim(User_Phone)))='10' then '('+SUBSTRING(User_Phone,1,3)+')'+' '+SUBSTRING(User_Phone,4,3)+'-'+SUBSTRING(User_Phone,7,4) when len(ltrim(rtrim(User_Phone)))='' then ' ' end AS User_Phone from users

Now phone number field will show data in this format (###) ###-####

I hope you will like this ms sql server tutorial to format the phone number in this format (###) ###-####

Read more...

Displaying data from database in gridview

In this tutorial we will learn how to select records from mssql server database and then display these records/data in ASP.NET Gridview Control. In .aspx page we have a .NET gridview control and it’s .cs side we are creating connection to database after creating connection, dataset, data adapter and data command objects, and then selecting records from database using select query and then binding gridview with these records/data using data adapter.

displaying_data_in_gridview.aspx

<asp:gridview id="GridView1" runat="server" width="100%">
<Columns>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
</Columns>
</asp:gridview>

displaying_data_in_gridview.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;


public partial class Tutorials_displaying_data_in_gridview : System.Web.UI.Page
{
string strcon = "server=localhost;database=tutorials-db; uid=adeel;password=pakistan; Connect Timeout=200; Max Pool Size=5000";


SqlConnection con = new SqlConnection();

SqlCommand com = new SqlCommand();

DataSet ds = new DataSet();

SqlDataAdapter da;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}


private void bindgrid()
{
ds.Clear();
con.ConnectionString = strcon;


com.Connection = con;

con.Open();

string strQuery = "select * from users";

da = new SqlDataAdapter(strQuery,con);

da.Fill(ds);


GridView1.DataSource = ds;

GridView1.DataBind();

con.Close();
}

}

Let’s understand .cs code

1) if (!IsPostBack) means bindgrid() function will be call if user refresh the webpage or posted back the webpage, this is very good for us because request will go to the database server if and only when page is posted back, not every time when page is loaded.
2) bindgrid() is a custom made function that will bind the data in gridview.
3) ds.Clear() means clean the dataset, we normally clean the dataset before using it just for precaution so that if dataset contains any old data then that old data will be deleted.
4) da = new SqlDataAdapter(strQuery,con); means we are Creating SqlDataAdapter object and then Passing SQL Query and Connection Object as Paramters to SqlDataAdapter object
5) da.Fill(ds) means filling dataset through data adapter.
6) GridView1.DataSource = ds; means assigning datasource to the gridview.

Note: - Don’t forget to include System.Data.SqlClient namespace in the list of namespaces because due to this namespace connection with mssql server will be established.

So this is the way to display the data in gridview in asp.net using C#
Read more...

Popup windows in Javascript

In this tutorial we will learn how to create popup windows in javascript. Before creating popup windows let's understand what are popup windows.

Popup windows are additional windows infect additional web browser windows opened from a webpage. These windows are created by using javascript as a client side programming language.

Popup windows are widely used for the advertisement, to exchange the banners, polling, requesting signup for news letter etc.

Now let's look over the code to develop popup windows in javascript.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>popup windows in javascript</title>
<script language="javascript" type="text/javascript">
function openPopupWindow()
{
window.open("http://www.google.com",'Google','height=200, width=500,top=100,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no');

}
</script>
</head>
<body>
<form>
<input type="button" value="Click to open Popup Windows in Javascript" onclick="openPopupWindow();">
</form>
</body>
</html>

First of all we made a custom function and give it a name openPopupWindow(), in this function we simply call the built-in function windows.open() and give some parameters to window.open() function which are used for specifications for popup window that is going to be open. In this example the first parameter is webpage/website name, second parameter is the name given to the popup window, third parameter is the height, fourth is the width, fifth parameter handles the top margin to display popup window in the current window, sixth one is self explanatory, seventh parameter means there will be no scrollbar in the popup windows that is going to be open, eighth parameter means there will be no status bar, and last parameter means there will be no menubar in the popup window.

Happy Coding, Keep Coding…!
Read more...

How to fire OnIndexChanged event of Dropdownlist in asp.net

Sometimes people working with asp dropdownlists < asp:DropDownList runat="server"> feel very angry when OnIndexChanged() event of asp dropdownlist not fire. People go to debug and they feel more angry when they find that debugger is not going to OnIndexChanged() event of asp dropdownlist.

The reason why OnIndexChanged() event of dropdownlist not fire is the missing of
autopastback="true" attribute in the asp dropdown list, so here is the solution


<asp:DropDownList ID="ddl_SearchOption" runat="server" autopastback="true">


So this is the way to fire the OnIndexChanged event of Dropdownlist in asp.net Read more...