Feeds:
Posts
Comments

Archive for March, 2010

Let we have a string[] array and a ArrayList of String. We would like to convert ArrayList to String[] .

static string[] userNameList;
ArrayList userNameAl = new ArrayList( );

userNameList = (string[])userNameAl.ToArray(typeof(string));

Read Full Post »

In Asp.Net 2.0 and Asp.Net 3.5 we have a Tag Inside Web.config file that is ConnectionString Tag.

<connectionStrings>
<add name=”MyConnStringconnectionString=”Server=ServerName;Database=DBName;Uid=UserId;Pwd=Passwd;providerName=”System.Data.SqlClient/>
</connectionStrings>

Now to get the Connection string defined in the web.config file you have to do like this in your .cs  file below code is based on C#

string Conn= ConfigurationManager.ConnectionStrings[“MyConnString“].ConnectionString;

In Vb.NET

Dim Conn as String = ConfigurationManager.ConnectionStrings(“MyConnString“).ConnectionString

Read Full Post »

This tutorial will show you how to delete a file on the disk using ASP.NET 2.0 and C#.NET

To delete a simple file on the disk, we will need to first import the System.IO namespace. The System.IO namespace contains the File.Delete() method and FileInfo type that we will use to perform our delete with.

using System.IO

We’ll put our code in the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it first checks to see if the file exists using the FileInfo type. If it exists it runs the File.Delete() method to delete it, otherwise it throws a FileNotFoundException which is caught by one of the catch statements below the try block.

protected void btnSubmit_Click(object sender, EventArgs e)
{

try {

FileInfo TheFile = new FileInfo(MapPath(“.”) + “\\” + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(“.”) + “\\” + txtFile.Text);
}

else {

throw new FileNotFoundException();
}

}
catch (FileNotFoundException ex) {

lblStatus.Text += ex.Message;

}
catch (Exception ex) {

lblStatus.Text += ex.Message;

}

}

We have one textbox, a Submit button, a label, and a checkbox on the front end for user interaction. The front end .aspx page looks something like this:

<table width=”600″ border=”0″ align=”center” cellpadding=”5″ cellspacing=”1″ bgcolor=”#cccccc”>

<tr>

<td width=”100″ align=”right” bgcolor=”#eeeeee”> File to Delete:</td>
<td align=”center” bgcolor=”#FFFFFF”>
<asp:TextBox ID=”txtFile” runat=”server”></asp:TextBox>
<br />
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” /><br /> &nbsp;
<asp:label ID=”lblStatus” runat=”server”></asp:label></td>

</tr>

</table>

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnSubmit_Click(object sender, EventArgs e)
{

try {

FileInfo TheFile = new FileInfo(MapPath(“.”) + “\\” + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(“.”) + “\\” + txtFile.Text);
}

else {

throw new FileNotFoundException();
}

}catch (FileNotFoundException ex) {

lblStatus.Text += ex.Message;

}
catch (Exception ex) {

lblStatus.Text += ex.Message;

}

}

Read Full Post »

Sometimes we may need to upload data files from user end to Server side. The following program will perform that task.

Here is the .aspx file code :

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”DataTransfer.aspx.cs” Inherits=”DataTransfer” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; >
<head runat=”server”>
<title>Data Transfer</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

<asp:FileUpload ID=”FileUpload1″ runat=”server” /><br />
<asp:Button ID=”upButton” runat=”server” OnClick=”upButton_Click” Text=”File Upload” />
<asp:Label ID=”lblTransferMsg” runat=”server”></asp:Label><br />

</div>
</form>
</body>
</html>

Here is the .cs code::

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class DataTransfer : System.Web.UI.Page
{

protected HtmlInputFile myInputFile;
SqlConnection connection = null;
static string fileNameWithLocation = “”;

protected void Page_Init(object sender, EventArgs e)
{
//connection = new SqlConnection(“Data Source=CSE;Initial Catalog=razibdb;Integrated Security=True”);
connection = new SqlConnection(“server=’SERVERNAME’;uid= ‘USERID’;” + “pwd=’PASSWORD’;database=DATABASE; Connect Timeout=10000”);
}

protected void Page_Load(object sender, EventArgs e)

{
}

protected void upButton_Click(object sender, EventArgs e)
{
HttpPostedFile myFile = FileUpload1.PostedFile;
if (myFile.FileName != “” && myFile.ContentLength > 0)
{
String ServerFileName = Path.GetFileName(myFile.FileName);
fileNameWithLocation = “C:\\” + ServerFileName;
myFile.SaveAs(fileNameWithLocation);

SendDataToTable( );
DeleteXLSFile( );
}
else
{
lblTransferMsg.ForeColor = System.Drawing.Color.Red;
lblTransferMsg.Text = “Select a File !!!”;
}
}

private void DeleteXLSFile( )
{
try
{
FileInfo theFile = new FileInfo(“C:\\a.xls”);
if (theFile.Exists)
File.Delete(“C:\\a.xls”);
else
throw new FileNotFoundException();
}
catch (FileNotFoundException fnfe)
{
Response.Write(fnfe.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

private void SendDataToTable( )
{
DeleteDataFromTable( );
String strSQL = “”;
strSQL = “Insert into ReportTable Select * FROM OPENROWSET”;
strSQL += “(‘Microsoft.Jet.OLEDB.4.0’,’Excel 8.0;Database=” + fileNameWithLocation + “;”;
strSQL += “HDR = YES ‘,’SELECT * FROM [a$]’)”;

SqlCommand myCommand = new SqlCommand(strSQL, connection);
try
{
connection.Open( );
myCommand.ExecuteNonQuery( );
connection.Close( );

lblTransferMsg.ForeColor = System.Drawing.Color.Green;
lblTransferMsg.Text = “Transfer success”;
}
catch (Exception ex) { Response.Write(ex.ToString()); }

}

private void DeleteDataFromTable( )
{
String strSQL = “”;
strSQL = “DELETE FROM ReportTable”;
SqlCommand myCommand = new SqlCommand(strSQL, connection);
try
{
connection.Open( );
myCommand.ExecuteNonQuery( );
connection.Close( );
}
catch (Exception ex) { Response.Write(ex.ToString( )); }
}

//before uploading u have to create the table

Read Full Post »