Archive

Archive for May, 2013

Export DataTable into Excell (Oledb)

string destinationFile = @”D:\Book21.xlsx”;
string templateFile = @”D:\Book1.xlsx”; \\ contain column defination only
System.IO.File.Copy(templateFile, destinationFile, true);
string connectionString = String.Format(@”Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=’Excel 8.0;HDR=Yes'”, destinationFile);

// Establish a connection to the data source.
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection(connectionString);
objConn.Open();

// Add two records to the table named ‘MyTable’.
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandText = “Insert into [Sheet1$] (FirstName, LastName, Phone)” +
” values (‘Sarvesh’, ‘Kumar’, ‘56565656’)”;
objCmd.ExecuteNonQuery();
objCmd.CommandText = “Insert into [Sheet1$] (FirstName, LastName, Phone)” +
” values (‘Sarvesh 1’, ‘Kumr 1’, ‘5656456456’)”;
objCmd.ExecuteNonQuery();

//Use a range with a specific address for select (like – [Sheet1$A1:B10]):

// Close the connection.
objConn.Close();
System.Windows.Forms.MessageBox.Show(“Data exported successfully”);

Categories: C#

mailSettings from ConfigSection

<system.net>
<mailSettings>
<smtp from=”sarvesh.kumar@gmail.com”>
<network host=”smtp.gmail.com” port=”587″ userName=”sarvesh.kumar@gmail.com” password=”test1234″ />
</smtp>
</mailSettings>
</system.net>

Categories: .Net

Upload file threw handler in Silverlight

May 8, 2013 1 comment

Here is code that can help you to upload file on server threw Silverlight.

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true;
dlg.Filter = “PDF Files (*.pdf)|*.pdf”;
if ((bool)dlg.ShowDialog())
{
foreach(FileInfo info in dlg.Files)
{
UploadFile file = new UploadFile();
file.FileID = Guid.NewGuid();
file.FileName = info.Name;
file.FileType = ((DocumentType)this.cboDocType.SelectedItem).Name;
file.RawData = info.OpenRead();
files.Add(file);

UploadFile(file);
}
}
}

private void UploadFile(UploadFile file)
{
UriBuilder ub = new UriBuilder(new Uri(HtmlPage.Document.DocumentUri, “FileUpload.ashx”));
ub.Query = string.Format(“fileID={0}&filename={1}”, file.FileID, file.FileName);

WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(file.RawData, e.Result);
e.Result.Close();
file.RawData.Close();
};
c.OpenWriteAsync(ub.Uri);

}

private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;

while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}

Class UploadFile

public class UploadFile
{
public Guid FileID { get; set; }
public string FileType { get; set; }
public string FileName { get; set; }
public int PacketCount { get; set; }
public int PacketsUploaded { get; set; }
public Stream RawData { get; set; }
public List Packets { get; set; }
}

Handler Code

public class FileUpload : IHttpHandler
{
private HttpContext ctx;
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString[“filename”].ToString();
string uploadPath = Path.Combine(context.Server.MapPath(“~/Upload”), filename);

using (FileStream fs = File.Create(uploadPath))
{
SaveFile(context.Request.InputStream, fs);
fs.Close();
}
}

private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
stream.Close();

}

public bool IsReusable
{
get
{
return false;
}
}
}

Categories: .Net, SilverLight