Friday, February 23, 2007

Delete the data through DataGrid

This is the program to delete data in the Datagrid.

[CODE]
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DeleteGrid
{
///


/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected DataTable table = new DataTable();
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=Databasename;uid=username;pwd=username");
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
FillDG();
BindDG();
}
}

public void FillDG()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Emp order by iEid",con);
da.Fill(table);
}

public void BindDG()
{
DataGrid1.DataSource=table;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string s11;
s11 =(e.Item.Cells[1].Text);//Getting The DateGird Cell Conetent
SqlCommand Dcmd =new SqlCommand("Delete from Emp WHERE iEid=" + Convert.ToInt32(s11),con);
con.Open();
Dcmd.ExecuteNonQuery();
FillDG();
BindDG();
}
}
}
[/CODE]

OutPut
Initial DataGrid

AfterDeleting a One Row




Thursday, February 22, 2007

Editing & Updating The Data Through Data Grid

This The simple program to edit the Data and store them in The DataBase Using DataGrid Control

DG1 Is Id For DataGrid Control,To Get The CancelCommand,EditCommand,UpdateCommand,
in the Property Windows Click   this button the Event, then select the above mentioned event.




main thing is dont forget to add this if(!IsPostBack) line,else each time event rised the page will be refreshed and new data will not be updated, old data only updated in the database

Codeing
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DyGrid
{
///


/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DG1;
SqlConnection con = new SqlConnection(@"Server=Servername;DataBase=databasename;uid=username;
pwd=password");
DataSet ds= new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
DBCon();
}

public void DBCon()
{
SqlDataAdapter da =new SqlDataAdapter("Select * From Emp",con);
da.Fill(ds,"EmpTable");
DG1.DataSource=ds.Tables[0];
DG1.DataBind();
DG1.DataKeyField = "iEid";
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.DG1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_CancelCommand);

this.DG1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_EditCommand_1);

this.DG1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler
(this.DG1_UpdateCommand_1);

this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DG1_UpdateCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string s1,s2,s3;
TextBox Eid = (TextBox)e.Item.Cells[1].Controls[0];
s1=Eid.Text;
TextBox Ename=(TextBox)e.Item.Cells[2].Controls[0];
s2=Ename.Text;
TextBox Esal=(TextBox)e.Item.Cells[3].Controls[0];
s3=Esal.Text;
SqlCommand cmd =new SqlCommand("Update Emp Set dSalary=" + s3+ " ,cEName ='" + s2 + "' WHERE iEid=" + Convert.ToInt32(s1),con);
con.Open();
cmd.ExecuteNonQuery();
DG1.EditItemIndex=-1;
DBCon();
con.Close();
}

private void DG1_EditCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DG1.EditItemIndex = e.Item.ItemIndex;
DBCon();
}

private void DG1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DG1.EditItemIndex=-1;
DBCon();
}
}
}

Output

Intial

After UpdateLink Button Triggered(Clicked)


Data Which Is To Be Updated


Updated Content



Wednesday, February 21, 2007

Displaying Table Content In The DataGrid

This is the program to display the Table Content in The DataGrid

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace DisplayTableContent
{
///


/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection con = new SqlConnection(@"server=servername;database=databasename;uid=username;pwd=password");
SqlDataAdapter sa = new SqlDataAdapter("Select * from tablename",con);
DataSet da=new DataSet();
sa.Fill(da,"CustomName");
DataGrid1.DataSource=da;
DataGrid1.DataBind();


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}



OutPut

To Set Template For DataGrid Right Click Over The Control And Select The AutoFormat From the Context menu and select your Scheme





Saturday, February 17, 2007

Sending Mail Using ASP.NET

Hi,
This is Simple Program To Send Mail to any Mail Id

using System.Web.Mail //Not Available in .NET 2005.

Control Name

From TextBox            = txtFrom
To TextBox                 = txtTo
Subject TextBox         =  txtSubject
Message TextBox       = txtMessage
MailStatus LabelBox  = lmailstatus



In your IIS Server You Have To Configure,

Step As Follow,

Step 1: Default SMTP Virtual Server.

Step 2:Right-click it.

Step 3:Select Properties.

Step 4:Select Access tab.

Step 5:Click Relay Button.

Step 6:Relay Restriction Dialog Box, only the list below Option Will Be Selected By Default.

Step7:If The local IP address: 127.0.0.1 is Entered Means leave that one else you have to add the above ip address in The Single Computer Ip address it's enough

Program 

Protected Sub bSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bSend.Click
Dim msg As New Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text)
Dim mOut As New Net.Mail.SmtpClient("127.0.01")
Try
mOut.Send(msg)
lmailstatus.Text = "Status Mail Sent"
Catch ex As Exception
lmailstatus.Text = ex.Message
End Try
End Sub



Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.