Wednesday, November 30, 2011

Row edit,delete,update in grid view in asp.net using c#

Title: Edit,Delete,Update Data in Grid view in Asp.net using c#.net

Description:
As per previous articles we have seen how to export grid view data to excel in asp.net and how to bind data to drop down list in grid view.Now i would like to explain how to do edit,update in grid view using c#.Before going to start we have to set the grid view properties i:e(AutogenerateDeleteButton,AutogenerateEditButton,AutogenerateUpdateButton) to enable the auto generate Edit,Delete,Update buttons. The given example will show how to do the CRUD functionality on grid view data
Grid View properties:
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateSelectButton="True">
</asp:GridView>

Bind the data to grid view:
//page load event
if(page.isPostback==false)
{
dataset ds=null;
ds=(dataset)session["ds1"];
//session will contain dataset ds1 with data selected by user
if(ds!=null)
{
gvemp.datasource=ds.Tables["employee"];
gvemp.DataBind();
}
}      
Row Editing:
When user clicks on edit button post backtakes place,row editing event of grid view will be executed.This event will provide row index
protected void gremp_rowediting(object sender,EventArgs e)
{
dataset ds=(dstaset)session["ds1"];
gvemp.editindex=e.new editindex;
//e.newedit index:- will be provide index of row for which edit button is selected
gvemp.Datasource=Ds.Table["employee"];
gvemp.Databind();
}
Row Updating:
This will perform post back,row-updating event procedure of grid view will be executed
protected void gvemp-rowupdating(Object sender,EvenArgs e)
{
Textbox txt=(Textbox)gvemp.Rows[e.RowIndex].cells[3].controls[0];
//here i will update the third cell data in grid view
int avg=int.parse(txt.Text);
Dataset ds=(dataset)session["ds1"]; 
ds.Tables["employee"].rows[e.Rowindex]["Average"]=avg;
ds.Tables["employee"].AcceptChanges();
session["Ds1"]=ds;
//it will overwrite the session of Dataset
//Rearrange Gridview
gvemp.editIndex=-1;
gvemp.Datasource=Ds.Tables["employee"];
gvemp.DataBind();
}
Row Deleting:
This will perform post back,row-deleting event procedure of grid view will be executed
protected void gvemp-rowdeleting(Object sender,EvenArgs e)
{
Dataset ds=(dataset)session["ds1"]; 
ds.Tables["employee"].rows[e.Rowindex].Delete();
ds.Tables["employee"].AcceptChanges();
session["Ds1"]=ds;
gvemp.Datasource=Ds.Tables["employee"];
gvemp.DataBind();
}

How to upload a file in asp.net using c#

Title:How to use upload control in asp.net

Description:
We can say "Sending file from client system to server system" as a definition .Mostly this control can be used as in  practical for uploading resume,image and attachment towards email.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>How to upload a file in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpLoad id="Fud" AlternateText="You cannot upload files" runat="server" />
<asp:Button id="btnupload" Text="Upload File" OnClick="btnupload_Click" runat="server" />
<asp:Label runat="server" Id="lblmessage" />
</form>
</body>
</html>

The properties and methods of File upload:

*has file--true-->File is upload
false-->File is not uploaded
*posted file--content type-->it returns file types
text/plain--->normal text file
text/xml---->XML file
application/msword-->doc file
save as(file path)--->it will save upload file in server system
Here i will shown to allow user to upload photos(image file type of gif) .For this i have taken file upload control and button server control in aspx page.


Code behind:

protected void btnupload_Click(object sender, EventArgs e)
{
Response.write(Fud.Hasfile);
Response.write(Fud.filename);
Response.write(Fud.posted file contenttype);
if(Fud.Hasfile)
{
if(Fud.Postedfile.ContentType=="image/gif")
{ 
Fud.postedfile.saveAs(server.mappath(".")+"\\"+Fud.filename);
lblmessage.text="upload successfully";
}
else
lblmessage.text="upload gif file";
}
else
lblmessagetext="file required";
}
}
Note:File upload control supports maximum of 4mb file size for uploading to server

Tuesday, November 29, 2011

Export data to excel in c#

Title:How to export grid view data to excel in asp.net using c#.net

Description:
Basically we can export the data to excel in two ways in asp.net with c#. One is using Download Format and Other one is using Microsoft.Office.Interop.Excel .In the Download format ,it  will render the grid view and export in XLST format.It is used when the data is export from grid view to excel.In Interop.Eexcel will create excel file dynamically and load the data into it.Exporting data to excel its pretty simple. Let's see how this can be done.For this i have taken data from database first then fill the existing data set.The following name space i have used for excel properties
Asp.Net Export to excel:
using Microsoft.Office.Interop.Excel;
Create Excel document:
Excel.Application App;
Excel.Workbook WorkBook;
Excel.Worksheet WorkSheet;
object misValue = System.Reflection.Missing.Value;
App = new Excel.Application();
WorkBook = App.Workbooks.Add(misValue);
WorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1);
Load the data into Excel file:
Here i will get the data into data set then it will load in to excel file using the following iteration.
for (int col = 0; col < dsreportdata.Tables[0].Columns.Count; col++)
{
for (int row = 0; row < dsreportdata.Tables[0].Rows.Count; row++)
{
WorkSheet.Cells[row + 12, col + 3] = dsreportdata.Tables[0].Rows[row].ItemArray[col].ToString();
}
}

Monday, November 28, 2011

uncheck the check box if another check box checked in check box list or radio button list

Here i will unchecked  items  in radio button list server control or check box list server control when we select on any item in this  .For this i have used a  singleitemcheck() function for each item in list control .This function makes functionality is very quick in your application

Aspx Page:
<asp:radiobuttonlist id="contentcheck" repeatdirection="Horizontal" runat="server">
<asp:listitem onclick="singleitemcheck(this);">Home</asp:listitem>
<asp:listitem onclick="singleitemcheck(this);">Pages</asp:listitem>
</asp:radiobuttonlist>

Java Script:
function singleitemcheck(ch) {
            var chkList = ch.parentNode.parentNode.parentNode;
            var chks = chkList.getElementsByTagName("input");
            for (var i = 0; i < chks.length; i++) {
            if (chks[i] != ch&& ch.checked) {
                chks[i].checked = false;

Static methods in csharp

A method declaring using the static keyword is a static method rest of all instance only.While defining static methods,we should not refer to any instance member of class directly,but we can use them by creating the object of the class.Non static members of a class cannot be referred from static blocks with out object reference.
Example:
class example
{
 int x=10;
 static int y=20;
 static void add()
  {
    example e=new example();
    console.writeline(e.x+y);
  }
}


Bel