Set (Assign) value to FileUpload control using C# and VB.Net in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

I am trying to fetch all the details of user entered after login from database and setting it in my textbox with an update button everything is working fine, I can fetch all the values from database except pdf file uploaded by user which is stored in database in binary form. Can you please suggest how can I do so.

here is my code it is giving error:

In "FileUpload1.PostedFile.ToString() = dr["file"].ToString();"

After Registration when user will login and fill his profile information such as marks and upload resume...etc.

Then whenever next time user will login first it will check whether user data is available in database or not if it is already available then all the data filled by the user will be shown in the textbox to the user and an update button will be shown to the user in case if he wants to update his data.

So that why I want to show to the user that he has already uploaded his resume with resume name in the label and if he wants to update his resume and upload another one.

Posted 2 months ago

Hi itsme,

You need to show your data of file in lable when you are going to update that time need to check whether label is empty or not accordingly to label existing data will insert else data will not insert. Refer below code i make some changes in your code.

HTML

<div style="scrollbar-highlight-color: aqua" onscroll="true">
    <table align="center" style="position: relative; top: 20px; margin-left: 35%; border-style: double;
        align-content: center">
        <tr>
            <td>
                <table align="center">
                    <tr>
                        <td>
                            <b>Job Seeker Name</b>
                        </td>
                        <td>
                            <asp:TextBox ID="jsname" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Address:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_add" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Phone no.:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_no" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Email:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_email" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>10th Marks(CGPA):</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_tenth" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>12th Marks(CGPA:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_twelve" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Graduation Marks(CGPA):</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txt_graduate" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Qualification:</b>
                        </td>
                        <td>
                            <asp:DropDownList ID="txt_quali" runat="server" class="form-control input-lg">
                                <asp:ListItem>--Select Qualification--</asp:ListItem>
                                <asp:ListItem>10th</asp:ListItem>
                                <asp:ListItem>12th</asp:ListItem>
                                <asp:ListItem>Graduate</asp:ListItem>
                                <asp:ListItem>Masters</asp:ListItem>
                                <asp:ListItem>Phd</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Upload Resume:</b>
                        </td>
                        <td>
                            <asp:FileUpload ID="FileUpload1" runat="server" />
                            <asp:Label ID="fname" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" CssClass="btn btn-info" />
                            <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click"
                                CssClass="btn btn-info" Visible="false" />
                        </td>
                    </tr>
                </table>
                <tr>
                    <td align="center">
                        <br />
                        <asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="Blue"></asp:Label>
                    </td>
                </tr>
    </table>
    <input type="hidden" runat="server" id="Id" />
</div>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data;

Code

C#

SqlConnection mycon = new SqlConnection(ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ToString());
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        mycon.Open();
        cmd.Connection = mycon;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select Name,email from Common_regn2 where email='" + Session["mail"].ToString() + "'";
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                jsname.Text = dr["Name"].ToString();
                jsname.ReadOnly = true;
                txt_email.Text = dr["email"].ToString();
                txt_email.ReadOnly = true;
            }
        }
        Session["name"] = jsname.Text.ToString();
    }

    if (!IsPostBack)
    {
        mycon.Close();
        mycon.Open();
        cmd.Connection = mycon;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select js_address,Contact,tenth,twelve,graduate,Qualification from Job_Seeker_Profile where email='" + Session["mail"].ToString() + "'";
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                txt_quali.SelectedValue = dr["Qualification"].ToString();
                txt_add.Text = dr["js_address"].ToString();
                txt_no.Text = dr["Contact"].ToString();
                txt_tenth.Text = dr["tenth"].ToString();
                txt_twelve.Text = dr["twelve"].ToString();
                txt_graduate.Text = dr["graduate"].ToString();
                fname.Text = dr["file"].ToString();
                btnsave.Visible = false;
                btnUpdate.Visible = true;
            }
        }
    }
}

protected void btnsave_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["Q_SS_regnConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "Insert into Job_Seeker_Profile (js_name,js_address,Contact,email,tenth,twelve,graduate,resumefile,Qualification) Values (@Name,@Address,@Contact,@email,@tenth,@twelve,@graduate,@file,@Qualification)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Id", Id.Value);
                    cmd.Parameters.AddWithValue("@Name", jsname.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@Address", txt_add.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@Contact", txt_no.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@email", txt_email.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@tenth", txt_tenth.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@twelve", txt_twelve.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@graduate", txt_graduate.Text.Trim().ToString());
                    cmd.Parameters.AddWithValue("@file", bytes);
                    cmd.Parameters.AddWithValue("@Qualification", txt_quali.Text.Trim().ToString());
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    lblMessage.Text = "Saved Successfully.";
                }
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}

protected void btnUpdate_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["Q_SS_regnConnectionString1"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "UPDATE Job_Seeker_Profile SET js_name=@Name,js_address=@Address,Contact=@Contact,email=@email,tenth=@tenth,twelve=@twelve,graduate=@graduate,resumefile=@file,Qualification=@Qualification WHERE email='" + Session["mail"].ToString() + "'";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    if (string.IsNullOrEmpty(fname.Text))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Id", Id.Value);
                        cmd.Parameters.AddWithValue("@Name", jsname.Text);
                        cmd.Parameters.AddWithValue("@Address", txt_add.Text);
                        cmd.Parameters.AddWithValue("@Contact", txt_no.Text);
                        cmd.Parameters.AddWithValue("@email", txt_email.Text);
                        cmd.Parameters.AddWithValue("@tenth", txt_tenth.Text);
                        cmd.Parameters.AddWithValue("@twelve", txt_twelve.Text);
                        cmd.Parameters.AddWithValue("@graduate", txt_graduate.Text);
                        cmd.Parameters.AddWithValue("@file", bytes);
                        cmd.Parameters.AddWithValue("@Qualification", txt_quali.Text);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        lblMessage.Text = "Updated Successfully.";
                    }
                    else
                    {
                        lblMessage.Text = "Already exist data.";
                    }
                }
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}