簡體   English   中英

C#ASP.NET無法將聯系頁面上傳到SQL Server 2008 R2

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

讓我首先說我是ASP的新手,並且在過去幾周里我已經盡力學習腳本。 我無法設置aspx.cs頁面以獲取要在服務器上發布的信息。 任何幫助將非常感激。

我已經包含了后端的代碼和下面的Web表單。 Web表單是一個頁面,需要收集文本字段,RadioButton,下拉列表和checkboxlist。

RadioButton - 詢問需要“是”或“否”答案的問題復選框列表 - 如果選中則插入1,如果未選擇則插入0。

需要能夠使用asp.net 2.0,IIS 6,C#,SQL Server 2008 R2。

我將所有實際ID替換為數字* *

后端

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

 <%@ 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>

這里有幾件事情,在一個答案中幾乎無法覆蓋,所以讓我至少在我的評論之前提出建議你選擇一本基本的ASP.NET書籍並介紹其中的一些基礎知識。 看看你的帖子,我認為你理解了一些事情,但還不夠廣泛。

首先,ExecuteInsert方法是一個問題,我猜它不是編譯。 您不能使用數字命名方法參數; 那不是合法的標識符。 但是,除此之外,那個巨大的參數列表只是一個需要修復的問題......這就把我們帶到另一個概念......將數據從頁面傳遞到后端代碼。

您的ASP.NET控件ID聲明不能只是一個數字ID,因為它會使ASP.NET生成中間聲明,使用它作為將控件公開給您的代碼隱藏的標識符。 但是,數字標識符不合法。 我強烈建議你將這些ID重命名為更具描述性和建設性的東西。

例如,雖然我不確定它應該保留什么,但是假設你用ID =“3”聲明的asp:TextBox應該包含一個姓氏。 然后,一個好的ID將是“LastName”,然后可以通過標識符LastName在您的代碼隱藏中訪問它,並通過Text屬性訪問其內容,例如LastName.Text。

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

..來自codebehind:..

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

顯然,您需要將其與您的應用程序相適應,並將其與其他輸入相同。

如果對所有這些ID重復該過程,則會發現當Page.IsPostBack值為True時,您可以通過自然機制訪問用戶在Page_Load事件中提供的值。 這使得您的頁面易於閱讀並且程序化更易於管理,因為您可以從輸入中收集數據並將其更清晰地提供給插入。

SQL語句中的標識符存在同樣的問題,盡管使用參數填充它實際上是一種很好的做法。

這是一個好的開始。 看看我的建議是否為您提供了一個起點,改進您迄今為止所做的工作,並了解您是否能夠更接近可行的解決方案。 祝好運。

編輯基於與OP的討論

這應該提供一個標記和代碼隱藏示例,演示了從ASP.NET頁面中從CheckBoxList,TextBox和ListBox中提取值的一種方法,並將這些值作為SqlParameters添加到SqlCommand以插入到虛構的示例表中。

**

樣本頁面的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>

代碼背后:

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();
        }
    }
}

討論

該頁面包含一個三項CheckBoxList,一個TextBox和一個ListBox。 用戶可以選擇CheckBoxList中的任何一個項目,將任何文本放在TextBox中,並從LIstBox中選擇一個值。 該按鈕使表單提交,導致代碼隱藏在回發模式下執行。

回發模式使SQL數據部分分為三部分:

  1. 無論CheckBoxList中檢查的項目數是多少,這些值都對應於(在此示例中)樣本數據庫中的Y或N值。 如果選中某個項目,則該值應為“Y”,如果不是,則為“N”。 代碼遍歷CheckBoxList中的所有三個ListItem,並添加Parameter對象,其名稱對應於SQL查詢中指定的Parameter字段,根據需要設置值“Y”或“N”。 “parmName”變量是一個便利變量,用於構造名為Value1,Value2,Value3的前三個參數的名稱。

  2. 下一個參數取自SampleTextBox控件的Text屬性。

  3. 最后一個參數取自所選ListBox ListItem的Value屬性。

使用收集的參數,將觸發SqlCommand對象的ExecuteNonQuery方法,並使用提供的值執行語句。

希望這說明了至少一種從ASP.NET表單中收集值並將它們寫入數據庫的方法。 未示出提供/建立正確連接串以及聲明/打開Connection對象的元素。 由於數據庫查詢僅用於演示,因此尚未完成針對數據庫的此代碼的實際測試,但它確實可以執行ExecuteNonQuery()方法調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM