You must Sign In to post a response.
  • Category: ASP.NET

    Help with uploading file in the database

    Hi Guys,

    I am using asp.net and vb.net...I have below code for upload button, but what I want is as soon as the file gets uploaded, it has to get saved in my database...it should be stored in the below format...

    I have label1 and label2 in my webform which are the id and category names in the database...and the database layout is

    UploadTable

    ID CATEGORY FILENAME

    So once the fiile is uploaded it has to store in the database table names UoloadTable and should save as....ID taken from Label1, category taken from label2 and filename of the file uploaded....and this file has to get stored in UPLOADS folder.
    Also the file name with id,name has to to be stored in database, not contents but just file name with its id
  • #765756
    Hi Lexi,

    If you want to save image in folder and save the id in database and based on id if you want to get back the file again then create folder in your project solution and while click on save button save the image in that folder, refer below sample code


    protected void btnInsert_Click(object sender, EventArgs e)
    {
    if (fileuploadimage.HasFile)
    {
    string str = Server.MapPath(@"Image/");
    fileuploadimage.SaveAs(str + fileuploadimage.FileName);

    //getting image name and store image name into Database.
    string img = fileuploadimage.FileName;
    try
    {
    con.Open();

    cmd = new SqlCommand("Insert_Employee_Details", con);
    cmd.CommandType = CommandType.StoredProcedure;


    cmd.Parameters.AddWithValue("@Ename", txtname.Text);
    cmd.Parameters.AddWithValue("@Job", txtjob.Text);
    cmd.Parameters.AddWithValue("@mgr", txtmgr.Text);
    cmd.Parameters.AddWithValue("@Sal", txtsal.Text);
    cmd.Parameters.AddWithValue("@Comm", txtcomm.Text);
    cmd.Parameters.AddWithValue("@Pic", img);
    cmd.Parameters.AddWithValue("@Dept_No", DeptNo);

    int n = cmd.ExecuteNonQuery();

    if (n == 1)
    {
    Response.Write("Record inserted sucessfully");
    txtname.Text = "";
    txtjob.Text = "";
    txtmgr.Text = "";
    txtsal.Text = "";
    txtcomm.Text = "";
    }
    num = 0;
    Bind_GV();
    }
    catch (Exception ex)
    {
    }
    finally
    {
    con.Close();
    con.Dispose();
    }
    }
    }


    If you want to display the saved images then you have to get he id of the image and based on that you will get the image from the created folder.

    Refer below link to know more about this,
    http://www.dotnetspider.com/resources/45091-Insert-Edit-Update-Delete-Get-back-Image-from-DataBase.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments