Shalu Shalini

Shalu Shalini

  • NA
  • 123
  • 16.7k

File Downloading without gridview

Mar 8 2017 12:50 AM
Hi sir,
 
 This is Shalini .,
 
                     I need to download file from sql server without using grid view .. Now am max(fid) for particular user id .. it may return last occurance of file what i pocessed, but i want to show all files of given user id and user may download the content of file selected file using the file id .. 
 
Sample of code what i am using now is :
 Asp.net sample :










 
C# sample:
protected void Page_Load(object sender, EventArgs e)
fnfileid();
string constr = ConfigurationManager.ConnectionStrings["Sqlconn"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT distinct Valid_Ids FROM PREVTXUSERVALIDATEDEMAILIDS where User_Id=@user_id and Valid_Ids<>'Temp' and Id=@fid"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@user_id", 1);
cmd.Parameters.AddWithValue("@fid", Session["fileid"]);
using (DataTable dt = new DataTable())
{
sda.Fill(dt);

//Build the Text file data.
string txt = string.Empty;

//foreach (DataColumn column in dt.Columns)
//{
// //Add the Header row for Text file.
// txt += column.ColumnName + "\t\t";
//}

//Add new line.
//txt += "\r\n";

foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
txt += row[column.ColumnName].ToString() + "\t\t";
}

//Add new line.
txt += "\r\n";
}

//Download the Text file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Valid_Export.txt");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(txt);
Response.Flush();
Response.End();
}
}
}
}
}
protected void fnfileid()
{
  string constr = ConfigurationManager.ConnectionStrings["Sqlconn"].ConnectionString;
  SqlConnection sqlconn = new SqlConnection(constr);
  SqlCommand sqlcomm = new SqlCommand("Select max(Id) from prevtxfileupload where User_Id=@user_id");
  using (SqlDataAdapter sqlda =new SqlDataAdapter())
  {
  sqlcomm.CommandType = CommandType.Text;
  sqlcomm.Parameters.AddWithValue("@user_id", Session["User_Id"]);
  sqlcomm.Connection = sqlconn;
  sqlconn.Open();
  Session["fileid"] = sqlcomm.ExecuteScalar();
  sqlconn.Close();
  }
}
                     Can you please help me to solve this issue.. Hope, thete is positive reply from you..
                     Thanks in advance and reply me asap..
 
Thanks & regards
--- Shalini P
 

Answers (3)