locked
How to insert record into a SQL Server table when uploading file using Ajax Control Toolkit AjaxFileUpload control RRS feed

  • Question

  • User-2129884208 posted

    Using an ASP.NET FileUpload control, I can upload files to a folder and insert their path into a SQL Server database table. Can someone please tell me how to do the same using Ajax Control Toolkit v 19.1 AjaxFileUpload control?

    C# code which I am using in a button click event to insert record in relational database tables is as follows. I am using Visual Studio 2019 CE, ASP.NET Web Tools 2019 - 16.3.286.43615 and SQL Server 2017 Express (v14.0.2027).

    I will be very thankful for your time and consideration.

    con = new SqlConnection(@"Data Source=QUANTUM\SQLEXPRESS; Initial Catalog=SurgicalVideosDB; Integrated Security=true;");
     
    cmd = new SqlCommand("SPInsertMultipleRecords", con);
    cmd.CommandType = CommandType.StoredProcedure;
     
    con.Open();
     
    DataTable Patient = new DataTable("udtPatient");
    Patient.Columns.Add("PatientTempID");
    Patient.Columns.Add("PatientName");
    Patient.Columns.Add("Diagnosis");
    Patient.Columns.Add("Investigations");
    Patient.Columns.Add("Complications");
    Patient.Columns.Add("FollowUp");
    Patient.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text);
     
    SqlParameter pm = cmd.Parameters.AddWithValue("@Patient", Patient);
    pm.SqlDbType = SqlDbType.Structured;
     
    DataTable Surgeries = new DataTable("udtSurgeries");
    Surgeries.Columns.Add("SFKPatientID");
    Surgeries.Columns.Add("SurgeryDate");
    Surgeries.Columns.Add("SurgicalProcedure");
    Surgeries.Columns.Add("HospitalName");
    Surgeries.Rows.Add(TextBox1.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text);
     
    SqlParameter sm = cmd.Parameters.AddWithValue("@Surgeries", Surgeries);
    sm.SqlDbType = SqlDbType.Structured;
     
    DataTable Media = new DataTable("udtMedia");
    Media.Columns.Add("MFKPatientID");
    Media.Columns.Add("MediaType");
    Media.Columns.Add("MediaName");
    Media.Columns.Add("MediaLink");
     
    foreach (HttpPostedFile fp in FileUpload1.PostedFiles)
    {
        try
        {
            if (fp.ContentLength > 0)
            {
                fp.SaveAs(Server.MapPath("~/Media/") + Path.GetFileName(fp.FileName));
                string MediaLink = "Media/" + Path.GetFileName(fp.FileName);
                string MediaName = Path.GetFileName(fp.FileName);
                string MediaExtension = Path.GetExtension(MediaName);
     
                if (MediaExtension.ToLower() == ".jpg" || MediaExtension.ToLower() == ".jpeg" || MediaExtension.ToLower() == ".png")
                {
                    MediaLink = "<img src=" + MediaLink + " width=460 />";
                }
                else if (MediaExtension.ToLower() == ".wmv" || MediaExtension.ToLower() == ".mp4" || MediaExtension.ToLower() == ".mpg" || MediaExtension.ToLower() == ".avi")
                {
                    MediaLink = "<Video width=400 Controls><Source src=" + MediaLink + " type=video/mp4></video>";
                }
     
                Media.Rows.Add(TextBox1.Text, TextBox10.Text, MediaName, MediaLink);
     
                Label13.Visible = true;
                Label13.Text = "Upload(s) Successful.";
            }
        }
        catch (Exception)
        {
            Label13.Visible = true;
            Label13.Text = "Upload(s) Failed.";
        }
    }
     
    SqlParameter mm = cmd.Parameters.AddWithValue("@Media", Media);
    mm.SqlDbType = SqlDbType.Structured;
     
    cmd.ExecuteNonQuery();
    con.Close();
     
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";
    TextBox4.Text = "";
    TextBox5.Text = "";
    TextBox6.Text = "";
    TextBox7.Text = "";
    TextBox8.Text = "";
    TextBox9.Text = "";



    Tuesday, November 19, 2019 9:43 AM

All replies

  • User288213138 posted

    Hi Irfan Soofi,

    Using an ASP.NET FileUpload control, I can upload files to a folder and insert their path into a SQL Server database table. Can someone please tell me how to do the same using Ajax Control Toolkit v 19.1 AjaxFileUpload control?

    If you want to insert file path into database, you can try below code: In the UploadComplete event, you can get the path, name and extension.

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" />
    
    protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
            {
                string path = Server.MapPath("~/Doc/") + e.FileName.ToString();
                AjaxFileUpload1.SaveAs(path);
                string name = e.FileName;
                string extension = Path.GetExtension(name);
                string insert = "INSERT INTO File VALUES (@path, @name, @extension)";
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(insert))
                    {
                        cmd.Parameters.AddWithValue("@path", path);
                        cmd.Parameters.AddWithValue("@name", name);
                        cmd.Parameters.AddWithValue("@extension", extension);
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }

    Best regards,

    Sam

    Wednesday, November 20, 2019 10:22 AM