Wednesday, June 27, 2012
Multiple FileUploading using Jquery:
Multiple FileUploading using Jquery:
Add Below Two Jquery file to ur Solution
1. jquery-1.3.2.js.
2.jquery.MultiFile.js .
Aspx page Code:
Text="Button" onclick="Button1_Click" />
Upload Multiple Files in ASP.NET Using jQuery
.Cs Code:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("MyFiles") + "\\" +
System.IO.Path.GetFileName(hpf.FileName));
Response.Write("File: " + hpf.FileName + " Size: " +
hpf.ContentLength + " Type: " + hpf.ContentType + " Uploaded Successfully");
}
}
}
catch (Exception ex)
{
}
}
Add Below Two Jquery file to ur Solution
1. jquery-1.3.2.js.
2.jquery.MultiFile.js .
Aspx page Code:
Text="Button" onclick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
hpf.SaveAs(Server.MapPath("MyFiles") + "\\" +
System.IO.Path.GetFileName(hpf.FileName));
Response.Write("File: " + hpf.FileName + " Size: " +
hpf.ContentLength + " Type: " + hpf.ContentType + " Uploaded Successfully");
}
}
}
catch (Exception ex)
{
}
}
Tuesday, June 26, 2012
how to use AutoCompleteExtender
Step 1 : Take a
TextBox , AutoCompleteExtender
<asp:TextBox runat="server" ID="txtInput" Width="300" />
<cc1:AutoCompleteExtender runat="server" ID="AutoComplete1"
TargetControlID="txtInput"
ServiceMethod="GetNames"
ServicePath="WebService.asmx"
MinimumPrefixLength="1"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="20"/>
Step 2 : Add
ScriptManager on page with tag
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService.asmx" />
Services>
asp:ScriptManager>
Step 3: Add a
Webservice file in your website by Right click on website->Add new Item
->Web Service and add this code inside it..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
}
[WebMethod]
public string[] GetNames(string prefixText, int count)
{
List<string> items = new List<string>(count);
DataSet ds = new DataSet();
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(cs))
{
string sql = "SELECT name FROM people WHERE name LIKE
'" +
prefixText + "%'";
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand
= new SqlCommand(sql, connection);
adapter.Fill(ds);
}
foreach (DataRow dr in ds.Tables[0].Rows)
{
items.Add(dr["name"].ToString());
}
return items.ToArray();
}
}
Download file using GridView in Asp.Net
In some of the cases we want to give file listing with download option.
Here I am explaining, How to download file using GridView in Asp.Net ?
For this task, I have created my Database table like this-
FileDetails(FileId,FileName,FilePath)
FileId - Auto generated filed, to maintain primary key for file
FileName - Name of Uploaded file
FilePath - Complete file path for uploaded file
Here is code-
.aspx code-
No Data Found.
In above code, I have taken 4 columns in GridView , but here FilePath is hidden, I am not showing FilePath.
I am passing FilePath value using CommandArgument property, this value will be used in server side code to download file. For button I have set CommandName="download" so that button click can be handled in RowCommand() event of GridView.
The output of this .aspx will be -
Now .cs code-
protected void grdFileDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//Code to download file
if (e.CommandName == "download")
{
string filepath = Convert.ToString(e.CommandArgument);
byte[] data = System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "Application/octet-stream");
Response.AddHeader("Content-Length", data.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filepath);
Response.BinaryWrite(data);
Response.End();
}
}
catch (Exception)
{
throw;
}
}
{
try
{
//Code to download file
if (e.CommandName == "download")
{
string filepath = Convert.ToString(e.CommandArgument);
byte[] data = System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "Application/octet-stream");
Response.AddHeader("Content-Length", data.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filepath);
Response.BinaryWrite(data);
Response.End();
}
}
catch (Exception)
{
throw;
}
}
In above code, I have implemented GridVIew_RowCommand() event , here first I am checking commandName value using e.CommandName.
To get particular file path here I am using e.CommandArgument like this-
string filepath = Convert.ToString(e.CommandArgument);
After gettting Filepath I have used Response object to download file like this-
Response.BinaryWrite(data);
Here data will contain binary data that will be downloaded as a file.
Now if you will click on download button then related file will be downloaded.
Gridview Editing
protected void btnShow_Click(object
sender, EventArgs e)
{
GridItemCollection
objGridSelectedItems = grdPartyTypeMaster.SelectedItems;
GridItem
grdItem = objGridSelectedItems[0];
txtPartyType.Text =
grdItem.Cells[2].Text.ToString();
Session["OUTLET_TP"]
= txtPartyType.Text;
txtPartyType.ReadOnly = true;
txtDescription.Text =
grdItem.Cells[3].Text.ToString();
ComRateType.SelectedValue =
grdItem.Cells[4].Text.ToString();
cmbGLCode.SelectedValue =
grdItem.Cells[5].Text.ToString();
cmbGrpCode.SelectedValue =
grdItem.Cells[6].Text.ToString();
comComm.SelectedValue =
grdItem.Cells[7].Text.ToString();
comPro.SelectedValue =
grdItem.Cells[8].Text.ToString();
}
Sunday, June 24, 2012
Image Upload as Byte data
Image Upload as Byte data
if (FileUpload1.HasFile)
{
string filename = FileUpload1.PostedFile.FileName;
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/" + FileUpload1.FileName));
ImageButton1.ImageUrl = "~/Data/" + FileUpload1.FileName;
string file = FileUpload1.FileName;
Response.Write("Uploaded" + file);
using (Stream strm = FileUpload1.PostedFile.InputStream)
{
byte[] buffer = null;
buffer = new byte[strm.Length];
strm.Read(buffer, 0, (int)strm.Length);
}
}
Friday, June 22, 2012
similarities and differences between an interface and an abstract
There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:
Feature
|
Interface
|
Abstract class
|
Multiple inheritance
|
A class may inherit several interfaces.
|
A class may inherit only one abstract class.
|
Default implementation
|
An interface cannot provide any code, just the signature.
|
An abstract class can provide complete, default code and/or just the details that have to be overridden.
|
Access Modfiers | An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, properties |
Core VS Peripheral
|
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
|
An abstract class defines the core identity of a class and there it is used for objects of the same type.
|
Homogeneity
|
If various implementations only share method signatures then it is better to use Interfaces.
|
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
|
Speed
|
Requires more time to find the actual method in the corresponding classes.
|
Fast
|
Adding functionality (Versioning)
|
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
|
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
|
Fields and Constants | No fields can be defined in interfaces | An abstract class can have fields and constrants defined |
Using the Code
Subscribe to:
Posts (Atom)