Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i uploaded a text file to database and now i need to get that data into a textbox(multiline)

code that i have written for file upload
========================================
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile == null || string.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream == null)
        {
            Label1.Text =
            "<br />Error - unable to upload file. Please try again.<br />";
        }
        else
        {
            string SQL = "INSERT INTO FileUpLoad values(@FileName, @DateTime, @MIME, @BinaryData)";
            SqlCommand cmd = new SqlCommand(SQL, con);
            cmd.Parameters.AddWithValue("@FileName", TextBox2.Text.Trim());
            cmd.Parameters.AddWithValue("@DateTime", DateTime.Now);
            cmd.Parameters.AddWithValue("@MIME", FileUpload1.PostedFile.ContentType);
            byte[] imageBytes = new byte[FileUpload1.PostedFile.InputStream.Length + 1];
            FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
            cmd.Parameters.AddWithValue("@BinaryData", imageBytes);
            con.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = "<br />File successfully uploaded - thank you.<br />";
            con.Close();
        }
        con.Close();
    }
Posted
Updated 5-May-11 4:27am
v2
Comments
Baji Jabbar 5-May-11 10:26am    
Does your file got uploaded ?
Arun_Reddy 9-May-11 5:57am    
yes...

1 solution

using(SqlCommand cmd = new SqlCommand("SELECT BinaryData FROM FileUpload", connection))
{
   byte[] file = (byte)cmd.ExexcuteScalar();
   MemoryStream ms = new MemoryStream(file, true);
   ms.Write(file, 0, file.Length);

   using(StreamReader reader = new StreamReader(ms))
   {
     textbox.Text = reader.ReadToEnd();
   }
}
 
Share this answer
 
Comments
fjdiewornncalwe 5-May-11 10:59am    
+5. Not much else to say. Just how I would do it as well.
Arun_Reddy 9-May-11 5:55am    
using (SqlCommand cmd = new SqlCommand("SELECT BinaryData FROM FileUpload where FileName=" + TextBox1.Text.ToString(), con))
{

con.Open();
byte[] file =(byte[])cmd.ExecuteScalar();
MemoryStream ms = new MemoryStream(file, true);
ms.Write(file, 0, file.Length);

using (StreamReader reader = new StreamReader(ms))
{
Label4.Text = cmd.ExecuteScalar().ToString();
TextBox2.Text = reader.ReadToEnd();
}
con.Close();
}


But i cant get the text file to my textbox.........? it shows empty...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900