hi every1
im trying to download a music file from the database SQLserver.which i upload in the database.. The Code below works fine in wen i create a new project of an ASP.Net web application..m using VS 2008
but it gives me an error wen i run the same code in WPF broser APplication(xbap).. it throws an error saying Null reference encountered. the line of error is HTTPcontext context=HTTPcontext.Current;
it goes in the else part of the loop and returns "Current is Null" ..how do i set current to !null Please help me.
below is the code
private void downloadfile()
{
string connectionstring = @"Data Source=01HW107887\SQLEXPRESS;AttachDbFilename=D:\m ainproject\Database\prjectDatabase.mdf;Integrated Security=True"; //ConfigurationManager.ConnectionStrings["prjectDatabaseConnectionString"].ToString();
SqlConnection con = new SqlConnection(connectionstring);
string SqlSelect = "select * from Audi where Song_name='" + txtsname.Text + "'";
DataSet ds = new DataSet();
SqlCommand mycommand = new SqlCommand(SqlSelect,con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(mycommand);
da.Fill(ds, "Audi");
string Filename;
if (ds != null)
{
string FileType = ds.Tables["Audi"].Rows[0]["ContentType"].ToString();
byte[] FileContent = (byte[])ds.Tables["Audi"].Rows[0]["song"];
Filename = ds.Tables["Audi"].Rows[0]["Song_name"].ToString();
HttpContext context = HttpContext.Current;
if (context != null)
{
context.Response.ClearContent();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename + "." + FileType);
context.Response.AddHeader("Content-Length", FileContent.Length.ToString());
context.Response.ContentType = FileType;
context.Response.OutputStream.Write(FileContent, 0, FileContent.Length);
context.Response.End();
}
else
{
System.Windows.Forms.MessageBox.Show("Current is null");
}