Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developing a page and want to insert the input1 and input2 from each different fields into the database column. I want insert the 2 image paths into 2 different column, such as input1 from field1 insert into column 1 and inout2 from field2 will insert into column 2. If I dont want upload image1 and only upload image2, image2 also will inserting into the column2. How to define them as in array?

XML
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
First Name : * <asp:TextBox ID="txtFirstName" runat="server" Width="200" EnableViewState="false"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please fill in your first name" ControlToValidate="txtFirstName" SetFocusOnError="true" ValidationGroup="fileUploadForm"></asp:RequiredFieldValidator>
<br>Last Name : * <asp:TextBox id="txtLastName" runat="server" Width="200" EnableViewState="false"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please fill in your last name" ControlToValidate="txtLastName" SetFocusOnError="true" ValidationGroup="fileUploadForm"></asp:RequiredFieldValidator><br/>
Email : * <asp:TextBox id="txtEmail" runat="server" Width="200" EnableViewState="false"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please fill in your Email" ControlToValidate="txtEmail" SetFocusOnError="true" ValidationGroup="fileUploadForm"></asp:RequiredFieldValidator>
<br/>Photo 1 To Upload : * <asp:FileUpload  ID="File1" runat="server"  /><br />
    <asp:CustomValidator ID="CustomValidator1" runat="server"
 ControlToValidate="File1" ErrorMessage="File must be less than 10MB" OnServerValidate="ValidateUploadedFile"></asp:CustomValidator>
<br>Photo 2 To Upload : *</td>
<asp:FileUpload  ID="File2" runat="server"  /><br />
    <asp:CustomValidator ID="CustomValidator2" runat="server"
 ControlToValidate="File2" ErrorMessage="File must be less than 10MB" OnServerValidate="ValidateUploadedFile2"></asp:CustomValidator>
 <input id="btnSubmit" type="submit" value="Upload Photo File" name="cmdUploadPhoto" runat="server" onserverclick="Submit"/>

</form>


protected void Page_Load(object sender, EventArgs e)
        {
        }
        bool mPassedValidation = false;
        protected System.Data.SqlClient.SqlConnection sqlConn;
        protected System.Data.SqlClient.SqlCommand sqlCmd;
        protected void Submit(object sender, EventArgs e)
        {
            string[] arrayFilePaths = new string[2];
            arrayFilePaths[0] = string.Empty;
            arrayFilePaths[1] = string.Empty;
            if (mPassedValidation)
            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        string fn = txtFirstName.Text + "_" + Path.GetFileNameWithoutExtension(hpf.FileName) +
                        "_" + Guid.NewGuid() + Path.GetExtension(hpf.FileName);
                        string serverPath = Server.MapPath("./data/" + fn);
                        hpf.SaveAs(serverPath);
                        arrayFilePaths[i] = serverPath;
                    }
                }
                string sqlStrFilePaths = "'{0}','{1}'";
                sqlStrFilePaths = string.Format(sqlStrFilePaths, arrayFilePaths[0], arrayFilePaths[1]);
                ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["MyConnectionString"];
                string connString = cs.ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connString);
                sqlConn.Open();
                string sSQL = "INSERT INTO tbHPDigitalPrint2011(FirstName,LastName,Email,PhotoFileName,VideoFileName,RegDate)" +
                    "VALUES ('" + txtFirstName.Text + "','" + txtLastName.Text + "', '" + txtEmail.Text + "', '" + sqlStrFilePaths + "', getDate())";

                SqlCommand objCmd = new SqlCommand(sSQL, sqlConn);
                try
                {
                    objCmd.ExecuteNonQuery();
                    Response.Redirect("thanks.aspx");
                }
                catch (System.Exception ex)
                {
                    ErrorMsg.Text += "<font color=red>An exception of type " + ex.GetType() + " was encountered while inserting the data.<BR>";
                    ErrorMsg.Text += "Message: " + ex.Message + "<BR>";
                    ErrorMsg.Text += "Record was not written to database.<BR><BR></font>";
                }
                finally
                {
                    sqlConn.Close();
                }

            }
        }

        protected void ValidateUploadedFile(object source, ServerValidateEventArgs args)
        {
            if (File1.HasFile)
            {
                HttpPostedFile postedfile = File1.PostedFile;
                int fileLength = postedfile.ContentLength;
                if (string.Compare(Path.GetExtension(File1.PostedFile.FileName), ".zip", true) != 0)
                {
                    CustomValidator1.ErrorMessage = "The uploading file must be a zip file";
                    args.IsValid = false;
                    mPassedValidation = false;
                }
                else if (fileLength > 10 * 1024 * 1024)
                {
                    CustomValidator1.ErrorMessage = "The uploading file must be less than 10MB";
                    args.IsValid = false;
                    mPassedValidation = false;
                }
                else
                {
                    args.IsValid = true;
                    mPassedValidation = true;
                }
            }
        }
Posted
Updated 4-Apr-11 21:01pm
v2

1 solution

You didn't mention the problem you're having. If you want to add the image paths to two different row, then execute the command twice with different parameter values. And for the values don't use concatenated string from UI elements, use SqlParameter[^] instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900