简体   繁体   中英

C# ASP.NET trouble uploading contact page to SQL Server 2008 R2

Let me start of by saying that I am new to ASP and have done my best to learn the script the past couple weeks. I am having trouble setting up an aspx.cs page to get the information to post on the server. Any help would be much appreciated.

I have included the code for the backend and the web form below. The web form is one page that needs to collect text fields, RadioButton, dropdownlist, and checkboxlist.

RadioButton - askes question that requires a Yes or No answer Checkboxlist - Inset a 1 if selected and a 0 if not selected.

Needs to be able to work with asp.net 2.0, IIS 6, C#, SQL Server 2008 R2.

I replaced all of the actual ID with numbers* *

Back End

public partial class _Default : System.Web.UI.Page
{
    public string GetConnectionString()
    {
        //sets the connection string from your web config file "ConnString" is the name of your Connection String
        return ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
    }

private void ExecuteInsert(string 1, string 2, string 3, string 4, string 5, string 6, string 7, string 8, string 9, string 10, string 11, string 12, string 13, string 14, string 15, string 16, string 17, string 18, string 19, string 20, string 21, string 22, string 23, string 24, string 25, string 26, string 27, string 28, string 29, string 30, string 31, string 32, string 33, string 34, string 35, string 36, string 37, string 38, string 39, string 40, string 41, string 42, string 43, string 44, string 45, string 46, string 47, string 48, string 49, string 50, string 51, string 52, string 53, string 54, string 55, string 56, string 57, string 58, string 59, string 60, string 61, string 62, string 63, string 64, string 65, string 66)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());
    string sql = "INSERT INTO webContactForm (1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66) VALUES (@1,  @2,  @3,  @4,  @5,  @6,  @7,  @8,  @9,  @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, @21, @22, @23, @24, @25, @26, @27, @28, @29, @30, @31, @32, @33, @34, @35, @36, @37, @38, @39, @40, @41, @42, @43, @44, @45, @46, @47, @48, @49, @50, @51, @52, @53, @54, @55, @56, @57, @58, @59, @60, @61, @62, @63, @64, @65, @66)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[66];
        //param[0] = new SqlParameter("@Contact_id", SqlDbType.Int, 20);
        param[0] = new SqlParameter("@1", SqlDbType.VarChar, 200);
        param[1] = new SqlParameter("@2", SqlDbType.VarChar, 200);
        param[2] = new SqlParameter("@3", SqlDbType.VarChar, 200);
        param[3] = new SqlParameter("@4", SqlDbType.VarChar, 200);
        param[4] = new SqlParameter("@5", SqlDbType.VarChar, 200);
        param[5] = new SqlParameter("@6", SqlDbType.VarChar, 200);
        param[6] = new SqlParameter("@7", SqlDbType.VarChar, 200);
        param[7] = new SqlParameter("@8", SqlDbType.VarChar, 200);
        param[8] = new SqlParameter("@9", SqlDbType.VarChar, 200);
        param[9] = new SqlParameter("@10", SqlDbType.VarChar, 200);
        param[10] = new SqlParameter("@11", SqlDbType.VarChar, 200);
        param[11] = new SqlParameter("@12", SqlDbType.VarChar, 200);
        param[12] = new SqlParameter("@13", SqlDbType.VarChar, 200);
        param[13] = new SqlParameter("@14", SqlDbType.VarChar, 200);
        param[14] = new SqlParameter("@15", SqlDbType.VarChar, 200);
        param[15] = new SqlParameter("@16", SqlDbType.VarChar, 200);
        param[16] = new SqlParameter("@17", SqlDbType.VarChar, 200);
        param[17] = new SqlParameter("@18", SqlDbType.VarChar, 200);
        param[18] = new SqlParameter("@19", SqlDbType.VarChar, 200);
        param[19] = new SqlParameter("@20", SqlDbType.VarChar, 200);
        param[20] = new SqlParameter("@21", SqlDbType.VarChar, 200);
        param[21] = new SqlParameter("@22", SqlDbType.VarChar, 200);
        param[22] = new SqlParameter("@23", SqlDbType.VarChar, 200);
        param[23] = new SqlParameter("@24", SqlDbType.VarChar, 200);
        param[24] = new SqlParameter("@25", SqlDbType.VarChar, 200);
        param[25] = new SqlParameter("@26", SqlDbType.VarChar, 200);
        param[26] = new SqlParameter("@27", SqlDbType.VarChar, 200);
        param[27] = new SqlParameter("@28", SqlDbType.VarChar, 200);
        param[28] = new SqlParameter("@29", SqlDbType.VarChar, 200);
        param[29] = new SqlParameter("@30", SqlDbType.VarChar, 200);
        param[30] = new SqlParameter("@31", SqlDbType.VarChar, 200);
        param[31] = new SqlParameter("@32", SqlDbType.VarChar, 200);
        param[32] = new SqlParameter("@33", SqlDbType.VarChar, 200);
        param[33] = new SqlParameter("@34", SqlDbType.VarChar, 200);
        param[34] = new SqlParameter("@35", SqlDbType.VarChar, 200);
        param[35] = new SqlParameter("@36", SqlDbType.VarChar, 200);
        param[36] = new SqlParameter("@37", SqlDbType.VarChar, 200);
        param[37] = new SqlParameter("@38", SqlDbType.VarChar, 200);
        param[38] = new SqlParameter("@39", SqlDbType.VarChar, 200);
        param[39] = new SqlParameter("@40", SqlDbType.VarChar, 200);
        param[40] = new SqlParameter("@41", SqlDbType.VarChar, 200);
        param[41] = new SqlParameter("@42", SqlDbType.VarChar, 200);
        param[42] = new SqlParameter("@43", SqlDbType.VarChar, 200);
        param[43] = new SqlParameter("@44", SqlDbType.VarChar, 200);
        param[44] = new SqlParameter("@45", SqlDbType.VarChar, 200);
        param[45] = new SqlParameter("@46", SqlDbType.VarChar, 200);
        param[46] = new SqlParameter("@47", SqlDbType.VarChar, 200);
        param[47] = new SqlParameter("@48", SqlDbType.VarChar, 200);
        param[48] = new SqlParameter("@49", SqlDbType.VarChar, 200);
        param[49] = new SqlParameter("@50", SqlDbType.VarChar, 200);
        param[50] = new SqlParameter("@51", SqlDbType.VarChar, 200);
        param[51] = new SqlParameter("@52", SqlDbType.VarChar, 200);
        param[52] = new SqlParameter("@53", SqlDbType.VarChar, 200);
        param[53] = new SqlParameter("@54", SqlDbType.VarChar, 200);
        param[54] = new SqlParameter("@55", SqlDbType.VarChar, 200);
        param[55] = new SqlParameter("@56", SqlDbType.VarChar, 200);
        param[56] = new SqlParameter("@57", SqlDbType.VarChar, 200);
        param[57] = new SqlParameter("@58", SqlDbType.VarChar, 200);
        param[58] = new SqlParameter("@59", SqlDbType.VarChar, 200);
        param[59] = new SqlParameter("@60", SqlDbType.VarChar, 200);
        param[60] = new SqlParameter("@61", SqlDbType.VarChar, 200);
        param[61] = new SqlParameter("@62", SqlDbType.VarChar, 200);
        param[62] = new SqlParameter("@63", SqlDbType.VarChar, 200);
        param[63] = new SqlParameter("@64", SqlDbType.VarChar, 200);
        param[64] = new SqlParameter("@65", SqlDbType.VarChar, 200);
        param[65] = new SqlParameter("@66", SqlDbType.VarChar, 200);
    param[66] = new SqlParameter("@67", SqlDbType.VarChar, 200);

        param[0].Value = 1;
        param[1].Value = 2;
        param[2].Value = 3;
        param[3].Value = 4;
        param[4].Value = 5;
        param[5].Value = 6;
        param[6].Value = 7;
        param[7].Value = 8;
        param[8].Value = 9;
        param[9].Value = 10;
        param[10].Value = 11;
        param[11].Value = 12;
        param[12].Value = 13;
        param[13].Value = 14;
        param[14].Value = 15;
        param[15].Value = 16;
        param[16].Value = 17;
        param[17].Value = 18;
        param[18].Value = 19;
        param[19].Value = 20;
        param[20].Value = 21;
        param[21].Value = 22;
        param[22].Value = 23;
        param[23].Value = 24;
        param[24].Value = 25;
        param[25].Value = 26;
        param[26].Value = 27;
        param[27].Value = 28;
        param[28].Value = 29;
        param[29].Value = 30;
        param[30].Value = 31;
        param[31].Value = 32;
        param[32].Value = 33;
        param[33].Value = 34;
        param[34].Value = 35;
        param[35].Value = 36;
        param[36].Value = 37;
        param[37].Value = 38;
        param[38].Value = 39;
        param[39].Value = 40;
        param[40].Value = 41;
        param[41].Value = 42;
        param[42].Value = 43;
        param[43].Value = 44;
        param[44].Value = 45;
        param[45].Value = 46;
        param[46].Value = 47;
        param[47].Value = 48;
        param[48].Value = 49;
        param[49].Value = 50;
        param[50].Value = 51;
        param[51].Value = 52;
        param[52].Value = 53;
        param[53].Value = 54;
        param[54].Value = 55;
        param[55].Value = 56;
        param[56].Value = 57;
        param[57].Value = 58;
        param[58].Value = 59;
        param[59].Value = 60;
        param[60].Value = 61;
        param[61].Value = 62;
        param[62].Value = 63;
        param[63].Value = 64;
        param[64].Value = 65;
        param[65].Value = 66;
        param[66].Value = 67;

        //create loop to insert
        for (int i = 0; i < param.Length; i++)
        {
            cmd.Parameters.Add(param[i]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Error occured while inserting:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        conn.Close();
    }
    }
    private object GetConnectionString(string p)
    {
throw new NotImplementedException();
    }
    protected void Button1_Click(object sender, EventArgs e){
        //call the method to execute insert to the database
        cmd.Parameters.Add(new SqlParameter("@MyParamter", "my value"));

        ClearControls(Page);
}
}

WEB FORM CODE:

 <%@ Page Language="C#"  AutoEventWireup="true" CodeFile="Default.aspx.cs"" Inherits="Default2" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">

 <head id="Head1" runat="server">
<title>Contact Us</title>

  </head>
   <body>
    <form id="form1" runat="server">
     <div>
      <table class="style1">
        <tr>
            <td class="style4">TextBox column 1:</td>
            <td class="style2">
                <asp:TextBox ID="1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 2:</td>
            <td class="style2">
                <asp:TextBox ID="LName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">3 TextBox column 3,4,5:</td>
            <td class="style2">
                <asp:TextBox ID="3" runat="server" Width="60px"></asp:TextBox>-
                <asp:TextBox ID="4" runat="server" Width="59px"></asp:TextBox>-
                <asp:TextBox ID="5" runat="server" Width="62px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 6:</td>
            <td class="style2">
                <asp:TextBox ID="6" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">ListItem column 7:</td>
            <td class="style2">
            <asp:DropDownList ID="7" runat="server"
                              AppendDataBoundItems="true">
            <asp:ListItem Value="-1">Select</asp:ListItem>
            <asp:ListItem>A</asp:ListItem>
            <asp:ListItem>B</asp:ListItem>
            <asp:ListItem>C</asp:ListItem>
            <asp:ListItem>D</asp:ListItem>
            <asp:ListItem>E</asp:ListItem>
            <asp:ListItem>F</asp:ListItem>
            <asp:ListItem>G</asp:ListItem>
            </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">RadioButton column 8</td>
            <td class="style2">
                <asp:RadioButton ID="Yes" runat="server" Checked="True" />
                <asp:RadioButton ID="No" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 9:</td>
            <td class="style2">
                <asp:TextBox ID="9" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 10:</td>
            <td class="style2">
                <asp:TextBox ID="ClientLName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">RadioButton Column 11:</td>
            <td class="style2">
                <asp:RadioButton ID="Female" runat="server" />
                <asp:RadioButton ID="Male" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="">3 TextBox column 12,13,14:</td>
            <td class="">
                <asp:TextBox ID="12" runat="server" Width="50px"></asp:TextBox>(mm)/<asp:TextBox ID="13" runat="server" Width="50px"></asp:TextBox>(dd)/<asp:TextBox ID="14" runat="server" Width="85px"></asp:TextBox>(yyyy)
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 15:</td>
            <td class="style2">
                <asp:TextBox ID="15" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 16:</td>
            <td class="style2">
                <asp:TextBox ID="16" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 17:</td>
            <td class="style2">
                <asp:TextBox ID="17" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">Dropdownlist column 18:</td>
            <td class="style2">
            <asp:DropDownList ID="18" runat="server"
                              AppendDataBoundItems="true">
         <asp:ListItem value="-1">Select State</asp:ListItem>
         <asp:ListItem value="AL">Alabama</asp:ListItem>
         <asp:ListItem value="AR">Arkansas</asp:ListItem>
         <asp:ListItem value="AZ">Arizona</asp:ListItem>
         <asp:ListItem value="CA">California</asp:ListItem>
         <asp:ListItem value="CO">Colorado</asp:ListItem>
         <asp:ListItem value="CT">Connecticut</asp:ListItem>
         <asp:ListItem value="DC">District of Columbia</asp:ListItem>
         <asp:ListItem value="DE">Delaware</asp:ListItem>
         <asp:ListItem value="FL">Florida</asp:ListItem>
         <asp:ListItem value="GA">Georgia</asp:ListItem>
         <asp:ListItem value="IA">Iowa</asp:ListItem>
         <asp:ListItem value="ID">Idaho</asp:ListItem>
         <asp:ListItem value="IL">Illinois</asp:ListItem>
         <asp:ListItem value="IN">Indiana</asp:ListItem>
         <asp:ListItem value="KS">Kansas</asp:ListItem>
         <asp:ListItem value="KY">Kentucky</asp:ListItem>
         <asp:ListItem value="LA">Louisiana</asp:ListItem>
         <asp:ListItem value="MA">Massachusetts</asp:ListItem>
         <asp:ListItem value="MD">Maryland</asp:ListItem>
         <asp:ListItem value="ME">Maine</asp:ListItem>
         <asp:ListItem value="MI">Michigan</asp:ListItem>
         <asp:ListItem value="MN">Minnesota</asp:ListItem>
         <asp:ListItem value="MO">Missouri</asp:ListItem>
         <asp:ListItem value="MS">Mississippi</asp:ListItem>
         <asp:ListItem value="MT">Montana</asp:ListItem>
         <asp:ListItem value="NC">North Carolina</asp:ListItem>
         <asp:ListItem value="ND">North Dakota</asp:ListItem>
         <asp:ListItem value="NE">Nebraska</asp:ListItem>
         <asp:ListItem value="NH">New Hampshire</asp:ListItem>
         <asp:ListItem value="NJ">New Jersey</asp:ListItem>
         <asp:ListItem value="NM">New Mexico</asp:ListItem>
         <asp:ListItem value="NV">Nevada</asp:ListItem>
         <asp:ListItem value="NY">New York</asp:ListItem>
         <asp:ListItem value="OH">Ohio</asp:ListItem>
         <asp:ListItem value="OK">Oklahoma</asp:ListItem>
         <asp:ListItem value="OR">Oregon</asp:ListItem>
         <asp:ListItem value="PA">Pennsylvania</asp:ListItem>
         <asp:ListItem value="RI">Rhode Island</asp:ListItem>
         <asp:ListItem value="SC">South Carolina</asp:ListItem>
         <asp:ListItem value="SD">South Dakota</asp:ListItem>
         <asp:ListItem value="TN">Tennessee</asp:ListItem>
         <asp:ListItem value="TX">Texas</asp:ListItem>
         <asp:ListItem value="UT">Utah</asp:ListItem>
         <asp:ListItem value="VA">Virginia</asp:ListItem>
         <asp:ListItem value="VT">Vermont</asp:ListItem>
         <asp:ListItem value="WA">Washington</asp:ListItem>
         <asp:ListItem value="WI">Wisconsin</asp:ListItem>
         <asp:ListItem value="WV">West Virginia</asp:ListItem>
         <asp:ListItem value="WY">Wyoming</asp:ListItem>
            </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">TextBox column 19:</td>
            <td class="style2">
                <asp:TextBox ID="19" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
        <td>
        <h1>
        Providing:
       </h1>
        </td>
        <td>

        </td>
        </tr>
        <tr>
        <td>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem Value="20">Want 1</asp:ListItem>
                <asp:ListItem Value="21">Want 2</asp:ListItem>
                <asp:ListItem Value="22">Want 3</asp:ListItem>
                <asp:ListItem Value="23">Want 4</asp:ListItem>
                <asp:ListItem Value="24">Want 5</asp:ListItem>
                <asp:ListItem Value="25">Want 6</asp:ListItem>
                <asp:ListItem Value="26">Want 7</asp:ListItem>
                <asp:ListItem Value="27">Want 8</asp:ListItem>
                <asp:ListItem Value="28">Want 9</asp:ListItem>
                <asp:ListItem Value="29">Want 10</asp:ListItem>
                <asp:ListItem Value="30">Want 11</asp:ListItem>
                <asp:ListItem Value="31">Want 12</asp:ListItem>
                <asp:ListItem Value="32">Want 13</asp:ListItem>
                <asp:ListItem Value="33">Want 14</asp:ListItem>
                <asp:ListItem Value="34">Want 15</asp:ListItem>
                <asp:ListItem Value="35">Want 16</asp:ListItem>
                <asp:ListItem Value="36">Want 17</asp:ListItem>
                <asp:ListItem Value="37">Want 18</asp:ListItem>
                <asp:ListItem Value="38">Want 19</asp:ListItem>
                <asp:ListItem Value="39">Want 20</asp:ListItem>
                <asp:ListItem Value="40">Want 21</asp:ListItem>
            </asp:CheckBoxList>
        </td>
        <td>
        <asp:CheckBoxList ID="CheckBoxList2" runat="server">
                <asp:ListItem Value="41">Want 22</asp:ListItem>
                <asp:ListItem Value="42">Want 23</asp:ListItem>
                <asp:ListItem Value="43">Want 24</asp:ListItem>
                <asp:ListItem Value="44">Want 25</asp:ListItem>
                <asp:ListItem Value="45">Want 26</asp:ListItem>
                <asp:ListItem Value="46">Want 27</asp:ListItem>
                <asp:ListItem Value="47">Want 28</asp:ListItem>
                <asp:ListItem Value="48">Want 29</asp:ListItem>
                <asp:ListItem Value="49">Want 30</asp:ListItem>
                <asp:ListItem Value="50">Want 31</asp:ListItem>
                <asp:ListItem Value="51">Want 32</asp:ListItem>
                <asp:ListItem Value="52">Want 33</asp:ListItem>
                <asp:ListItem Value="53">Want 34</asp:ListItem>
                <asp:ListItem Value="54">Want 35</asp:ListItem>
                <asp:ListItem Value="55">Want 36</asp:ListItem>
                <asp:ListItem Value="56">Want 37</asp:ListItem>
                <asp:ListItem Value="57">Want 38</asp:ListItem>
                <asp:ListItem Value="58">Want 39</asp:ListItem>
                <asp:ListItem Value="59">Want 40</asp:ListItem>
                <asp:ListItem Value="60">Want 41</asp:ListItem>
                <asp:ListItem Value="61">Want 42</asp:ListItem>
            </asp:CheckBoxList>
        </td>
        </tr>
        <tr>
            <td class="style4">Comment TextBox column 62:</td>
            <td class="style2">
                <asp:TextBox ID="62" runat="server"></asp:TextBox>
            </td>
        </tr>
    <tr>
            <td>
              <asp:CheckBoxList ID="CheckBoxList3" runat="server">
                <asp:ListItem Value="63">Have 1</asp:ListItem>
                <asp:ListItem Value="64">Have 2</asp:ListItem>
                <asp:ListItem Value="65">Have 3</asp:ListItem>
              </asp:CheckBoxList>
    </td>
            <td>
              <asp:CheckBoxList ID="CheckBoxList4" runat="server">
                <asp:ListItem Value="66">Have 4</asp:ListItem>
                <asp:ListItem Value="67">Have 5</asp:ListItem>
              </asp:CheckBoxList>
    </td>
        </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Submit" 
                         onclick="Button1_Click" />
</form>

</body>
</html>

There are several things at hand here, almost too much to cover in a single answer, so let me at least preface my comments by suggesting you pick up a basic ASP.NET book and go through some of the basics therein. Looking at your posting, I think you understand a few things, but not broadly enough just yet.

First, the ExecuteInsert method is a problem, and I'm guessing it isn't compiling. You can't name method parameters with a number; that's not a legal identifier. But, beyond that, that huge parameter list is just a problem begging to be fixed....and that brings us to the other notion...getting the data from the page to the back-end code.

Your ASP.NET Control ID declarations cannot be just a numeric ID, because it causes ASP.NET to generate intermediate declarations using that as an identifier that exposes the control to your code-behind. However, numeric identifiers are not legal. I would strongly suggest you rename those ID's to something more descriptive and constructive.

For example, while I'm not sure what its supposed to hold, let's say the asp:TextBox you've declared with ID = "3" is supposed to hold a Last Name. A good ID would, then, be "LastName," which can then be accessed in your codebehind via the identifier LastName, and its contents via the Text property, eg LastName.Text.

<asp:TextBox ID="LastName" runat="server" Width="60px"></asp:TextBox>

.. from codebehind: ..

// access the value of the textbox as (purely an example):
if (LastName.Text=="")
{
    // Do something with a blank last name
};

Obviously, you'd need to adapt this to your application, and the same notion to the other inputs.

If you repeat that process for all those ID's, you then realize you have a natural mechanism to access the values provided by a user in the Page_Load event when the Page.IsPostBack value is True. That makes your page much easier to read and programmaticallly easier to manage, because you can collect the data from your inputs and supply them to your Insert more cleanly.

The same problem exists with the identifiers in your SQL statement, although using parameters to populate it is actually a good practice.

That's a good start for now. See if what I've suggested gives you a starting point, refine what you've done so far, and see if you can get closer to a workable solution. Good luck.

EDIT based on discussion with OP

This should provide a markup and codebehind sample that demonstrates one way to extract the values from a CheckBoxList, a TextBox, and a ListBox from an ASP.NET page and adds those values as SqlParameters to a SqlCommand for insertion into a fictional sample table.

**

ASPX Markup for sample page:

**

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage.aspx.cs" Inherits="DemoPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:CheckBoxList id="SampleCheckBox" runat="server">
        <asp:ListItem Text="First Value" Value="1"></asp:ListItem>
        <asp:ListItem Text="Second Value" Value="2"></asp:ListItem>
        <asp:ListItem Text="Third Value" Value="3"></asp:ListItem>
    </asp:CheckBoxList>
    <br />
    <asp:TextBox ID="SampleTextBox" Width="50" runat="server"></asp:TextBox>
    <asp:ListBox ID="SampleListBox" runat="server">
        <asp:ListItem Text="First Value" Value="A"></asp:ListItem>
        <asp:ListItem Text="Second Value" Value="B"></asp:ListItem>
        <asp:ListItem Text="Third Value" Value="C"></asp:ListItem>
    </asp:ListBox>
    <asp:Button id="SubmitButton" runat="server" UseSubmitBehavior="true" />
    </div>
    </form>
</body>
</html>

Code behind:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DemoPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {

            string strSql = @"Insert Into SampleTable (Check1, Check2, Check3, Text, ListBoxResult) 
                                               Values (@Value1, @Value2, @Value3, @TextValue, @ListResult)";
            SqlCommand cmd = new SqlCommand(strSql);

            // Handle CheckBoxList values
            int checkValue=0;
            foreach (ListItem sampleItem in SampleCheckBox.Items)
            {
                checkValue++; //increment ID used to build parameter name
                string parmName=String.Format("@Value{0}",checkValue);
                SqlParameter newParameter = new SqlParameter();
                newParameter.ParameterName = parmName;
                if (sampleItem.Selected)
                {
                    newParameter.Value = "Y";
                    //column value in DB for field corresponding to sampleItem is "Y"
                }
                else
                {
                    newParameter.Value="N";
                    //column value in DB for field corresponding to sampleItem is "n"
                }
                cmd.Parameters.Add(newParameter);
            }

            // Handle TextBox value
            cmd.Parameters.Add("@TextValue", SampleTextBox.Text);

            // Handle ListBox selected value
            cmd.Parameters.Add("@ListResult", SampleListBox.SelectedValue);

            cmd.ExecuteNonQuery();
        }
    }
}

Discussion

The page hosts one three-item CheckBoxList, one TextBox, and one ListBox. The user may select any one of the items in the CheckBoxList, place any text in the TextBox, and select a value from the LIstBox. The button causes the form to submit, causing the codebehind to execute in a postback mode.

The postback mode causes the SQL data portion to fire in three parts:

  1. Regardless of the number of items checked in the CheckBoxList, the values correspond (in this example) to a Y or N value in the sample database. If an item is checked, the value should be "Y", and "N" if it is not. The code iterates through all three ListItems in the CheckBoxList, and adds Parameter objects with names corresponding to the Parameter fields named in the SQL query, setting values of "Y" or "N" as needed. The "parmName" variable is a convenience variable to construct the names of the first three parameters, named Value1, Value2, Value3.

  2. The next parameter is taken from the Text property of the SampleTextBox control.

  3. The last parameter is taken from the Value property of the selected ListBox ListItem.

With the parameters collected, the ExecuteNonQuery method of the SqlCommand object is fired, executing the statement with the provided values.

Hopefully, this illustrates at least one way of collecting values from an ASP.NET form and writing them to a database. The elements of supplying/buiding the proper connection string, as well as declaring/opening a Connection object, are not illustrated. As the database query is only for demonstration, actual test of this code against a database has not been accomplished, but it does work up to the ExecuteNonQuery() method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM