简体   繁体   English

C#ASP.NET无法将联系页面上传到SQL Server 2008 R2

[英]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. 让我首先说我是ASP的新手,并且在过去几周里我已经尽力学习脚本。 I am having trouble setting up an aspx.cs page to get the information to post on the server. 我无法设置aspx.cs页面以获取要在服务器上发布的信息。 Any help would be much appreciated. 任何帮助将非常感激。

I have included the code for the backend and the web form below. 我已经包含了后端的代码和下面的Web表单。 The web form is one page that needs to collect text fields, RadioButton, dropdownlist, and checkboxlist. Web表单是一个页面,需要收集文本字段,RadioButton,下拉列表和checkboxlist。

RadioButton - askes question that requires a Yes or No answer Checkboxlist - Inset a 1 if selected and a 0 if not selected. RadioButton - 询问需要“是”或“否”答案的问题复选框列表 - 如果选中则插入1,如果未选择则插入0。

Needs to be able to work with asp.net 2.0, IIS 6, C#, SQL Server 2008 R2. 需要能够使用asp.net 2.0,IIS 6,C#,SQL Server 2008 R2。

I replaced all of the actual ID with numbers* * 我将所有实际ID替换为数字* *

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: WEB表格代码:

 <%@ 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. 这里有几件事情,在一个答案中几乎无法覆盖,所以让我至少在我的评论之前提出建议你选择一本基本的ASP.NET书籍并介绍其中的一些基础知识。 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. 首先,ExecuteInsert方法是一个问题,我猜它不是编译。 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. 您的ASP.NET控件ID声明不能只是一个数字ID,因为它会使ASP.NET生成中间声明,使用它作为将控件公开给您的代码隐藏的标识符。 However, numeric identifiers are not legal. 但是,数字标识符不合法。 I would strongly suggest you rename those ID's to something more descriptive and constructive. 我强烈建议你将这些ID重命名为更具描述性和建设性的东西。

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. 例如,虽然我不确定它应该保留什么,但是假设你用ID =“3”声明的asp:TextBox应该包含一个姓氏。 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. 然后,一个好的ID将是“LastName”,然后可以通过标识符LastName在您的代码隐藏中访问它,并通过Text属性访问其内容,例如LastName.Text。

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

.. from codebehind: .. ..来自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. 如果对所有这些ID重复该过程,则会发现当Page.IsPostBack值为True时,您可以通过自然机制访问用户在Page_Load事件中提供的值。 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. SQL语句中的标识符存在同样的问题,尽管使用参数填充它实际上是一种很好的做法。

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 编辑基于与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. 这应该提供一个标记和代码隐藏示例,演示了从ASP.NET页面中从CheckBoxList,TextBox和ListBox中提取值的一种方法,并将这些值作为SqlParameters添加到SqlCommand以插入到虚构的示例表中。

** **

ASPX Markup for sample page: 样本页面的ASPX标记:

** **

<%@ 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. 该页面包含一个三项CheckBoxList,一个TextBox和一个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. 用户可以选择CheckBoxList中的任何一个项目,将任何文本放在TextBox中,并从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: 回发模式使SQL数据部分分为三部分:

  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. 无论CheckBoxList中检查的项目数是多少,这些值都对应于(在此示例中)样本数据库中的Y或N值。 If an item is checked, the value should be "Y", and "N" if it is not. 如果选中某个项目,则该值应为“Y”,如果不是,则为“N”。 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. 代码遍历CheckBoxList中的所有三个ListItem,并添加Parameter对象,其名称对应于SQL查询中指定的Parameter字段,根据需要设置值“Y”或“N”。 The "parmName" variable is a convenience variable to construct the names of the first three parameters, named Value1, Value2, Value3. “parmName”变量是一个便利变量,用于构造名为Value1,Value2,Value3的前三个参数的名称。

  2. The next parameter is taken from the Text property of the SampleTextBox control. 下一个参数取自SampleTextBox控件的Text属性。

  3. The last parameter is taken from the Value property of the selected ListBox ListItem. 最后一个参数取自所选ListBox ListItem的Value属性。

With the parameters collected, the ExecuteNonQuery method of the SqlCommand object is fired, executing the statement with the provided values. 使用收集的参数,将触发SqlCommand对象的ExecuteNonQuery方法,并使用提供的值执行语句。

Hopefully, this illustrates at least one way of collecting values from an ASP.NET form and writing them to a database. 希望这说明了至少一种从ASP.NET表单中收集值并将它们写入数据库的方法。 The elements of supplying/buiding the proper connection string, as well as declaring/opening a Connection object, are not illustrated. 未示出提供/建立正确连接串以及声明/打开Connection对象的元素。 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. 由于数据库查询仅用于演示,因此尚未完成针对数据库的此代码的实际测试,但它确实可以执行ExecuteNonQuery()方法调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SQL Server 2008 R2在ASP.NET C#中创建Crystal Report - Creating Crystal Report in asp.net c# with SQL Server 2008 R2 部署使用Asp.Net,C#和Sql Server 2008 R2构建的Web应用程序 - Deploying an Web Application built using Asp.Net,C# and Sql Server 2008 R2 使用SqlParameter实现C#ASP.NET和SQL Server 2008 R2的安全性 - Using SqlParameter for security with C# ASP.NET and SQL Server 2008 R2 具有SQL 2008 R2开发人员和SQL Express 2008 R2的ASP.Net C#ASPNETDB.mdb - ASP.Net C# ASPNETDB.mdb with SQL 2008 R2 Developers and SQL Express 2008 R2 使用asp.net C#在sql server 2008中上传文档 - Uploading documents in sql server 2008 using asp.net C# asp.net中Windows Server 2008 R2上的PDF缩略图 - PDF Thumbnails on windows server 2008 R2 in asp.net 30秒后在ASP.NET上超时,在SQL Server 2008 R2上没有超时 - Timeout on ASP.NET after 30 seconds, on SQL server 2008 R2 no timeout 存储过程不返回任何数据asp.net sql server 2008 r2 - store procedure don't return any data asp.net sql server 2008 r2 使用C#安装SQL Server 2008 R2 Express - Install SQL Server 2008 R2 Express using C# 具有现有SQL Server 2008 R2数据库的C#应用 - C# App with Existing SQL Server 2008 R2 Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM