Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. saving the other information to database if no content is present in file upload control using asp.net with c#

saving the other information to database if no content is present in file upload control using asp.net with c#

Scheduled Pinned Locked Moved ASP.NET
csharptutorialasp-netdatabase
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    developerit
    wrote on last edited by
    #1

    hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code

    SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
    cmdsave.CommandType = CommandType.StoredProcedure;
    con.Open();

            cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text);
            cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text);
    
            cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value);
            
            cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text);
            cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value);
    
            cmdsave.Parameters.AddWithValue("@City", txtcity.Text);
            cmdsave.Parameters.AddWithValue("@Area", txtarea.Text);
    
            cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text);
    
            cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text);
            cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text);
            cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
    

    FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
    byte[]branchcontent=new byte[branchinfo.Length];
    FileStream branchstream = branchinfo.OpenRead();
    branchstream.Read(branchcontent,0,branchcontent.Length);
    branchstream.Close();
    cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
    cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);

    cmdsave.Parameters.AddWithValue("@Active", x);
    if (cmdsave.ExecuteNonQuery()!= 0)
    {

                Functions.ShowAlertMessage("saved");
    
                con.Close();
                
            }
    
    S P 2 Replies Last reply
    0
    • D developerit

      hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code

      SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
      cmdsave.CommandType = CommandType.StoredProcedure;
      con.Open();

              cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text);
              cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text);
      
              cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value);
              
              cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text);
              cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value);
      
              cmdsave.Parameters.AddWithValue("@City", txtcity.Text);
              cmdsave.Parameters.AddWithValue("@Area", txtarea.Text);
      
              cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text);
      
              cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text);
              cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text);
              cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
      

      FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
      byte[]branchcontent=new byte[branchinfo.Length];
      FileStream branchstream = branchinfo.OpenRead();
      branchstream.Read(branchcontent,0,branchcontent.Length);
      branchstream.Close();
      cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
      cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);

      cmdsave.Parameters.AddWithValue("@Active", x);
      if (cmdsave.ExecuteNonQuery()!= 0)
      {

                  Functions.ShowAlertMessage("saved");
      
                  con.Close();
                  
              }
      
      S Offline
      S Offline
      Shahriar Iqbal Chowdhury Galib
      wrote on last edited by
      #2

      hi, use the code below,

      if (FileUpload1.HasFile)
      {
      FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
      byte[] branchcontent = new byte[branchinfo.Length];
      FileStream branchstream = branchinfo.OpenRead();
      branchstream.Read(branchcontent, 0, branchcontent.Length);
      branchstream.Close();
      cmdsave.Parameters.AddWithValue("@BranchPicture", (branchcontent == null ? DBNull.Value : branchcontent));
      }

      Hope this will help

      1 Reply Last reply
      0
      • D developerit

        hi , iam using asp.net with c# i want to store the other information to database even the user doesnot select the image in file upload control .how to do can you give example which helps me. the code works fine when the user selects the image , but when user doesnt select the image it gives errror ,but i want to store the other information if user doesnt selects the image the code is here please correct the code

        SqlCommand cmdsave = new SqlCommand("AddBranchProc11", con);
        cmdsave.CommandType = CommandType.StoredProcedure;
        con.Open();

                cmdsave.Parameters.AddWithValue("@BranchId", txtbranchid.Text);
                cmdsave.Parameters.AddWithValue("@BranchName", txtBranchname.Text);
        
                cmdsave.Parameters.AddWithValue("@CompanyId", ddlcompanyid1.SelectedItem.Value);
                
                cmdsave.Parameters.AddWithValue("@ContractNo", txtcontractno.Text);
                cmdsave.Parameters.AddWithValue("@Region", ddlRegions.SelectedItem.Value);
        
                cmdsave.Parameters.AddWithValue("@City", txtcity.Text);
                cmdsave.Parameters.AddWithValue("@Area", txtarea.Text);
        
                cmdsave.Parameters.AddWithValue("@Address", txtaddress.Text);
        
                cmdsave.Parameters.AddWithValue("@StartDate", txtstartdate1.Text);
                cmdsave.Parameters.AddWithValue("@EndDate", txtEnd.Text);
                cmdsave.Parameters.AddWithValue("@ContractReminder", ddlcontractreminder.SelectedItem.Value);
        

        FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
        byte[]branchcontent=new byte[branchinfo.Length];
        FileStream branchstream = branchinfo.OpenRead();
        branchstream.Read(branchcontent,0,branchcontent.Length);
        branchstream.Close();
        cmdsave.Parameters.AddWithValue("@ReminderType", ddlremindertype.SelectedItem.Value);
        cmdsave.Parameters.AddWithValue("@BranchPicture",branchcontent);

        cmdsave.Parameters.AddWithValue("@Active", x);
        if (cmdsave.ExecuteNonQuery()!= 0)
        {

                    Functions.ShowAlertMessage("saved");
        
                    con.Close();
                    
                }
        
        P Offline
        P Offline
        Paramhans Dubey
        wrote on last edited by
        #3

        I have made some changes in the above reply

        if (FileUpload1.HasFile)
        {
        FileInfo branchinfo = new FileInfo(FileUpload1.PostedFile.FileName.Trim());
        byte[] branchcontent = new byte[branchinfo.Length];
        FileStream branchstream = branchinfo.OpenRead();
        branchstream.Read(branchcontent, 0, branchcontent.Length);
        branchstream.Close();
        cmdsave.Parameters.AddWithValue("@BranchPicture", (branchcontent == null ? DBNull.Value : branchcontent));
        }
        else
        {
        cmdsave.Parameters.AddWithValue("@BranchPicture", DBNull.Value);
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups