• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 777
  • Last Modified:

ASP.NET Panel Control

Hello Experts,

I'm creating an application that needs to display different asp.net controls based on a value selected from a RadioButtonList Control. If you look below at the rbl_PreventiveHealthScreenings_SelectedIndexChanged code below you will see that I need to display different panels based on what a user selects from the RadioButtonList control.

The problem that I'm having is that I have two TextBox Controls that need to insert a date into a field called "phs_date". But I can't figure out how I can code this part so it only inserts the date based on the RadioButtonList value selected. Notice the line I have below for my ButtonClick Event, I thought this would work but it does now.


                        if (string.IsNullOrEmpty(txtOnsiteDateCompleted.Text) || (string.IsNullOrEmpty(txtOffsiteDateCompleted.Text)))
                        {
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOffsiteDateCompleted.Text;
                        }



HTML:

            <article>
                <h1>Preventive Health Screenings</h1>
                <p class="warning">All fields are required.</p>
                <asp:Label ID="lblPreventiveHealthScreenings" runat="server" Text="I completed my Preventive Screenings as provided by PA Cyber." CssClass="label"></asp:Label><br />
                <asp:RadioButtonList ID="rbl_PreventiveHealthScreenings" CssClass="radiolisttext" runat="server" TextAlign="right" OnSelectedIndexChanged="rbl_PreventiveHealthScreenings_SelectedIndexChanged" AutoPostBack="True"></asp:RadioButtonList>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_PreventiveHealthScreening" runat="server" ControlToValidate="rbl_PreventiveHealthScreenings" CssClass="warning" ErrorMessage="required"></asp:RequiredFieldValidator><br /><br />
                <asp:Panel ID="panel_Onsite" runat="server">
                    <asp:Label ID="lblOnsiteDateCompleted" runat="server" Text="Date Completed" CssClass="label"></asp:Label><br />
                    <asp:TextBox ID="txtOnsiteDateCompleted" runat="server" CssClass="textbox"></asp:TextBox>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_OnsiteDateCompleted" runat="server" ControlToValidate="txtOnsiteDateCompleted" CssClass="warning" ErrorMessage="required"></asp:RequiredFieldValidator><br /><br />
                </asp:Panel>
                <asp:Panel ID="panel_Offsite" runat="server">
                    <asp:Label ID="lblOffsiteDateCompleted" runat="server" Text="Date Completed" CssClass="label"></asp:Label><br />
                    <asp:TextBox ID="txtOffsiteDateCompleted" CssClass="textbox" runat="server"></asp:TextBox>&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_OffsiteDateCompleted" runat="server" ControlToValidate="txtOffsiteDateCompleted" CssClass="warning" ErrorMessage="required"></asp:RequiredFieldValidator><br /><br />
                    <asp:FileUpload ID="fu_OffsiteFileUpload" CssClass="fileupload" runat="server" />&nbsp;&nbsp;<asp:RequiredFieldValidator ID="rfv_OffsiteFileUpload" runat="server" ControlToValidate="fu_OffsiteFileUpload" CssClass="warning" ErrorMessage="required"></asp:RequiredFieldValidator>&nbsp;&nbsp;<asp:Label ID="lblVerificationFormTypeError" CssClass="info" runat="server"></asp:Label><br /><br />
                    <asp:Label ID="lblVerificationFormFileName" CssClass="success" runat="server"></asp:Label>
                    <asp:Label ID="lblFileSize" CssClass="error" runat="server"></asp:Label>
                </asp:Panel>
                <br />
                <br />
                <asp:CheckBox ID="cb_PreventiveScreenings" runat="server" CssClass="checkboxtext" Text=" I agree - that all information has been completed to the best of my knowledge." />
                <br />
                <br />
                <asp:HiddenField ID="hf_emp_id" runat="server" />
                <asp:HiddenField ID="hf_phs_id" runat="server" />
                <asp:Button ID="btn_PreventiveScreenings" runat="server" Text="Submit" CssClass="button" OnClick="btn_PreventiveScreenings_Click" />
                <br />
                <br />
                <asp:Label ID="lblInsertError" runat="server" CssClass="error"></asp:Label>
            </article>



CodeBehind:

    protected void rbl_PreventiveHealthScreenings_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rbl_PreventiveHealthScreenings.SelectedItem.Value == "20000")
            panel_Onsite.Visible = true;
        if (rbl_PreventiveHealthScreenings.SelectedItem.Value == "20001")
            panel_Offsite.Visible = true;
    }



protected void btn_PreventiveScreenings_Click(object sender, EventArgs e)
    {
            if (Page.IsValid && cb_PreventiveScreenings.Checked == true) // make sure all fields have data and cb_AnnualPhysical is Checked before Inserting to DB.
            {
                if (fu_OffsiteFileUpload.HasFile)  // a file has been uploaded
                {
                    //Make sure we are dealing a .pdf file only
                    string extension = Path.GetExtension(fu_OffsiteFileUpload.PostedFile.FileName).ToLower();
                    string MIMEType = null;

                    switch (extension)
                    {
                        case ".pdf":
                            MIMEType = "application/pdf";
                            break;
                        default:
                            lblVerificationFormTypeError.Visible = true;
                            lblVerificationFormTypeError.Text = "PDF files only!";
                            return;
                    }

                    string filename = fu_OffsiteFileUpload.PostedFile.FileName.Split(new char[] { '\\' }).Last();
                    int fileSize = fu_OffsiteFileUpload.PostedFile.ContentLength;

                    // Allow only files less than 1,048,576 bytes (approximately 1 MB) to be uploaded.
                    if ((fileSize < 1048576))
                    {
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandText = "InsertPreventiveHealthScreenings";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = conn;

                        // Load PDF InputStream into Byte array
                        byte[] imageBytes = new byte[fu_OffsiteFileUpload.PostedFile.InputStream.Length + 1];
                        fu_OffsiteFileUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);

                        cmd.Parameters.AddWithValue("@psv_id", SqlDbType.Int).Value = rbl_PreventiveHealthScreenings.SelectedItem.Value;
                        cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = hf_emp_id.Value;
                        cmd.Parameters.AddWithValue("@phs_pdf_file", SqlDbType.Image).Value = imageBytes;
                        cmd.Parameters.AddWithValue("@phs_pdf_filename", SqlDbType.VarChar).Value = filename;
                        cmd.Parameters.AddWithValue("@phs_pdf_mime", SqlDbType.VarChar).Value = MIMEType;
                        cmd.Parameters.AddWithValue("@phs_pdf_size", SqlDbType.VarChar).Value = fileSize;
                        cmd.Parameters.AddWithValue("@phs_section_complete", SqlDbType.Int).Value = cb_PreventiveScreenings.Checked;

                        if (string.IsNullOrEmpty(txtOnsiteDateCompleted.Text) || (string.IsNullOrEmpty(txtOffsiteDateCompleted.Text)))
                        {
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOffsiteDateCompleted.Text;
                        }

                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();
                        }

                        catch (Exception ex)
                        {
                            lblInsertError.Visible = true;
                            lblInsertError.Text = ("Error on insert: " + ex.Message.ToString());
                        }

                        finally
                        {
                            Response.Redirect("index.aspx");
                            conn.Close();
                        }
                    }

                    else
                    {
                        lblFileSize.Visible = true;
                        lblFileSize.Text = "File size must be 1MB or smaller";
                    }
                }
                else  // no file has been uploaded, we only need to update txtPhysicalDateCompleted
                {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "InsertPreventiveHealthScreenings";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;

                    byte[] imageBytes = new byte[1];

                    cmd.Parameters.AddWithValue("@psv_id", SqlDbType.Int).Value = rbl_PreventiveHealthScreenings.SelectedItem.Value;
                    cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = hf_emp_id.Value;
                    cmd.Parameters.AddWithValue("@phs_pdf_file", SqlDbType.Image).Value = imageBytes;
                    cmd.Parameters.AddWithValue("@phs_pdf_filename", SqlDbType.VarChar).Value = "";
                    cmd.Parameters.AddWithValue("@phs_pdf_mime", SqlDbType.VarChar).Value = "application/octet-stream";
                    cmd.Parameters.AddWithValue("@phs_pdf_size", SqlDbType.VarChar).Value = 0;
                    cmd.Parameters.AddWithValue("@phs_section_complete", SqlDbType.Int).Value = cb_PreventiveScreenings.Checked;

                    if (string.IsNullOrEmpty(txtOnsiteDateCompleted.Text))
                    {
                        cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = DBNull.Value;
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;
                    }

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }

                    catch (Exception ex)
                    {
                        lblInsertError.Visible = true;
                        lblInsertError.Text = ("Error on insert: " + ex.Message.ToString());
                    }

                    finally
                    {
                        Response.Redirect("index.aspx");
                        conn.Close();
                    }
                }
            }
    }
0
asp_net2
Asked:
asp_net2
  • 2
1 Solution
 
informaniacCommented:
Something like?

           if(panel_Onsite.Visible == true)
              cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;

Open in new window

0
 
asp_net2Author Commented:
Hi informaniac,

I tried your solution below in my Button Click Event but nothing was put into the DB :(


CodeBehind:

protected void btn_PreventiveScreenings_Click(object sender, EventArgs e)
    {
            if (Page.IsValid && cb_PreventiveScreenings.Checked == true) // make sure all fields have data and cb_AnnualPhysical is Checked before Inserting to DB.
            {
                if (fu_OffsiteFileUpload.HasFile)  // a file has been uploaded
                {
                    //Make sure we are dealing a .pdf file only
                    string extension = Path.GetExtension(fu_OffsiteFileUpload.PostedFile.FileName).ToLower();
                    string MIMEType = null;

                    switch (extension)
                    {
                        case ".pdf":
                            MIMEType = "application/pdf";
                            break;
                        default:
                            lblVerificationFormTypeError.Visible = true;
                            lblVerificationFormTypeError.Text = "PDF files only!";
                            return;
                    }

                    string filename = fu_OffsiteFileUpload.PostedFile.FileName.Split(new char[] { '\\' }).Last();
                    int fileSize = fu_OffsiteFileUpload.PostedFile.ContentLength;

                    // Allow only files less than 1,048,576 bytes (approximately 1 MB) to be uploaded.
                    if ((fileSize < 1048576))
                    {
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandText = "InsertPreventiveHealthScreenings";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = conn;

                        // Load PDF InputStream into Byte array
                        byte[] imageBytes = new byte[fu_OffsiteFileUpload.PostedFile.InputStream.Length + 1];
                        fu_OffsiteFileUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);

                        cmd.Parameters.AddWithValue("@psv_id", SqlDbType.Int).Value = rbl_PreventiveHealthScreenings.SelectedItem.Value;
                        cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = hf_emp_id.Value;
                        cmd.Parameters.AddWithValue("@phs_pdf_file", SqlDbType.Image).Value = imageBytes;
                        cmd.Parameters.AddWithValue("@phs_pdf_filename", SqlDbType.VarChar).Value = filename;
                        cmd.Parameters.AddWithValue("@phs_pdf_mime", SqlDbType.VarChar).Value = MIMEType;
                        cmd.Parameters.AddWithValue("@phs_pdf_size", SqlDbType.VarChar).Value = fileSize;
                        cmd.Parameters.AddWithValue("@phs_section_complete", SqlDbType.Int).Value = cb_PreventiveScreenings.Checked;

                        if (panel_Onsite.Visible == true)
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;

                        if (panel_Offsite.Visible == true)
                            cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;

                        //if (string.IsNullOrEmpty(txtOnsiteDateCompleted.Text))
                        //{
                        //    cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = DBNull.Value;
                        //}
                        //else
                        //{
                        //    cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;
                        //}

                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();
                        }

                        catch (Exception ex)
                        {
                            lblInsertError.Visible = true;
                            lblInsertError.Text = ("Error on insert: " + ex.Message.ToString());
                        }

                        finally
                        {
                            Response.Redirect("index.aspx");
                            conn.Close();
                        }
                    }

                    else
                    {
                        lblFileSize.Visible = true;
                        lblFileSize.Text = "File size must be 1MB or smaller";
                    }
                }
                else  // no file has been uploaded, we only need to update txtPhysicalDateCompleted
                {
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WellnessTracker"].ConnectionString);

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "InsertPreventiveHealthScreenings";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;

                    byte[] imageBytes = new byte[1];

                    cmd.Parameters.AddWithValue("@psv_id", SqlDbType.Int).Value = rbl_PreventiveHealthScreenings.SelectedItem.Value;
                    cmd.Parameters.AddWithValue("@emp_id", SqlDbType.Int).Value = hf_emp_id.Value;
                    cmd.Parameters.AddWithValue("@phs_pdf_file", SqlDbType.Image).Value = imageBytes;
                    cmd.Parameters.AddWithValue("@phs_pdf_filename", SqlDbType.VarChar).Value = "";
                    cmd.Parameters.AddWithValue("@phs_pdf_mime", SqlDbType.VarChar).Value = "application/octet-stream";
                    cmd.Parameters.AddWithValue("@phs_pdf_size", SqlDbType.VarChar).Value = 0;
                    cmd.Parameters.AddWithValue("@phs_section_complete", SqlDbType.Int).Value = cb_PreventiveScreenings.Checked;

                    if (panel_Onsite.Visible == true)
                        cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;

                    if (panel_Offsite.Visible == true)
                        cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOffsiteDateCompleted.Text;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }

                    catch (Exception ex)
                    {
                        lblInsertError.Visible = true;
                        lblInsertError.Text = ("Error on insert: " + ex.Message.ToString());
                    }

                    finally
                    {
                        Response.Redirect("index.aspx");
                        conn.Close();
                    }
                }
            }
    }
0
 
itmarsolutionsCommented:
try this

if (rbl_PreventiveHealthScreenings.SelectedItem.Value == "20000")
{
cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOnsiteDateCompleted.Text;
}
 elseif (rbl_PreventiveHealthScreenings.SelectedItem.Value == "20001")
{
cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = txtOffsiteDateCompleted.Text;
}
else
{
cmd.Parameters.AddWithValue("@phs_date", SqlDbType.DateTime).Value = DBNull.Value;
}
0
 
asp_net2Author Commented:
@itmarsolutions,

Yes, that worked out perfectly!!!

Now, for another question. I now need to be able display the value that was selected in the RadioButtonList Control along with the values based in the Panel Control from the selected RadioButtonList Control value.

Could you help me with this? If so, I will create another post.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now