0

i am trying to get files as bytes from table and show , i am getting the files as dataset but when trying to move it as datatable it shows row as zero and moves out of the if condition

 protected void btnview_Click(object sender, EventArgs e)
    {
        string code = txtbudid.Text;
        try
        {
            {
                using (MasterManagement mMgmt = new MasterManagement(General.ConnString()))
                {
                    dsBud = mMgmt.getfile(code);


                    DataTable dt = new DataTable();
                    dsBud.Tables.Add(dt);
                    if (dsBud != null)
                    {
                        if (dt.Rows.Count > 0)
                        {
                            byte[] bytes = (byte[])dt.Rows[0]["filedata"];
                            Response.Buffer = true;
                            Response.Charset = "";
                            Response.Cache.SetCacheability(HttpCacheability.NoCache);
                            //Response.ContentType = dt.Rows[]
                            Response.AddHeader("Content-disposition", "attachment;filename=" + dt.Rows[0]["filename"].ToString());
                            Response.BinaryWrite(bytes);
                            Response.Flush();
                            Response.End();
                        }

                    }

                }


            }
  • 2
    You need to use a DataTable that is (presumably) already in `dsBud`. You are adding a new DataTable and then expecting it to have rows. Do you understand why it will not have any rows? – Crowcoder Jul 07 '20 at 22:44
  • 1
    `dsBud.Tables.Add(dt);` this code will not add rows to dt. – Mohammed Sajid Jul 07 '20 at 22:44
  • DataTable dt = dsBud.Tables[0]; i have used this method to take rows from the table , now i am getting "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." while response.flush any suggestion – user9680560 Jul 07 '20 at 22:46
  • @sajit thanks i have removed that line DataTable dt = dsBud.Tables[0]; i have used this method to take rows from the table , now i am getting "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." while response.flush any suggestion – user9680560 Jul 07 '20 at 22:47
  • Do not END. You may be closing connection before the other end has received all the data., – jdweng Jul 07 '20 at 23:14
  • @user9680560 sajid not sajit :), you should move ``Response.End();`` out of `using`, take a look here : https://stackoverflow.com/questions/10756359/unable-to-evaluate-expression-on-web-page – Mohammed Sajid Jul 07 '20 at 23:49

0 Answers0