Sulaiman Sulaiman - 4 years ago 133
ASP.NET (C#) Question

lable don't show the message

i have problem with the lable when i upload file into database it not show any message

c#

protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["homeworkConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles (FileName,ContentType,Number,Date,Data) values (@Name, @ContentType,@number,getDate(), @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@number", Session["id"].ToString());
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
fname.Visible = true;
fname.Text = "file Has been uploaded";
}
}

}


asp.net code

<asp:Label ID="fname" runat="server" Text="Label" Visible="False"></asp:Label>


the lable work with insert value but not work with the uploading file

Answer Source
    protected void btnUpload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["homeworkConnectionString2"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into tblFiles (FileName,ContentType,Number,Date,Data) values (@Name, @ContentType,@number,getDate(), @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@number", Session["id"].ToString());
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

                }

            }
        }

                fname.Visible = true;
                fname.Text = "file Has been uploaded";

    }

Try Like this...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download