簡體   English   中英

如何從文本框和下拉列表將值插入數據庫

[英]How to insert values into database from textbox and dropdownlist

我不太擅長.net或sql。

問題是我有一個Web表單和一個數據庫。 該表格將允許用戶在文本框和下拉列表中輸入信息。 下拉列表中的數據將保存在表格中。

因此,我正在從表中讀取值,當用戶填寫表格並從下拉列表中選擇所需的選項時,應將文本框中的數據和選定的下拉列表發送回去,以保存在數據庫中。

我已經從數據庫中成功讀取了該值,並將其顯示在下拉列表中,如以下代碼所示:

  public class state { public string stateID { get; set; } public string stateName { get; set; } } [WebMethod] public static List<state> PopulateDropDownList() { DataTable dt = new DataTable(); List<state> objDept = new List<state>(); SqlConnection con = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true"); { using (SqlCommand cmd = new SqlCommand("SELECT STATE_ID, STATE_Name FROM state", con)) { con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { objDept.Add(new state { stateID = dt.Rows[i]["STATE_ID"].ToString(), stateName = dt.Rows[i]["STATE_Name"].ToString() }); } } return objDept; } } } 
 <script src=" http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", //url is the path of our web method (Page name/functionname) url: "Default.aspx/PopulateDropDownList", data: "{}", dataType: "json", //called on jquery ajax call success success: function (result) { $('#ddlstate').empty(); $('#ddlstate').append("<option value='0'>-Select-</option>"); $.each(result.d, function (key, value) { $("#ddlstate").append($("<option></option>").val(value.stateID).html(value.stateName)); }); }, //called on jquery ajax call failure error: function ajaxError(result) { alert(result.status + ' : ' + result.statusText); } }); }); </script><p>State</p> <asp:DropDownList ID="ddlstate" runat="server" Width="160px" /> 

但是,即使我成功調用了要顯示在下拉列表中的數據,也無法將所選數據以及文本框中的數據重新插入數據庫中。 換句話說,數據不會保存到數據庫中。

這是我在“單擊提交時”插入數據的代碼:

  public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == true) { Label1.Text = ("**Thanks for entering your information"); } } protected void Button1_Click(object sender, EventArgs e) { using (SqlConnection vid = new SqlConnection("Data Source = ****; Initial Catalog = LCF2016; Integrated Security = true")) { vid.Open(); using (SqlCommand xp = new SqlCommand("insert into LCF2016 (Fname, Lname, Email, Birthdate, Phone, Address, City, STATE_ID, Zip, Country_ID, Days_Per_Month, Primary_Language, Secondary_Language, Occupation_ID, HearAbout_ID, Other_Skills) Values(@Fname, @Lname, @Email, @Birthdate, @Phone, @Address, @City, @STATE_ID, @Zip, @Country_ID, @Days_Per_Month, @Primary_Language, @Secondary_Language, @Occupation_ID, @HearAbout_ID @Other_Skills)", vid)) { xp.Parameters.AddWithValue("@Fname", TextBox1.Text); xp.Parameters.AddWithValue("@Lname", TextBox2.Text); xp.Parameters.AddWithValue("@Email", TextBox3.Text); xp.Parameters.AddWithValue("@Birthdate", TextBox4.Text); xp.Parameters.AddWithValue("@Phone", TextBox5.Text); xp.Parameters.AddWithValue("@Address", TextBox6.Text); xp.Parameters.AddWithValue("@City", TextBox7.Text); xp.Parameters.AddWithValue("@STATE_ID", ddlstate.SelectedValue); xp.Parameters.AddWithValue("@Zip", TextBox8.Text); xp.Parameters.AddWithValue("@country_ID", ddlcountry.SelectedValue); xp.Parameters.AddWithValue("@Days_Per_Month", TextBox10.Text); xp.Parameters.AddWithValue("@Primary_Language", ddllangp.SelectedValue); xp.Parameters.AddWithValue("@Secondary_Language", ddllangs.SelectedValue); xp.Parameters.AddWithValue("@Occupation_ID", ddloccup.SelectedValue); xp.Parameters.AddWithValue("@HearAbout_ID", ddlhearabout.SelectedValue); xp.Parameters.AddWithValue("@Other_Skills", TextBox15.Text); xp.ExecuteNonQuery(); } } 

我得到的錯誤是

無效的回發或回調參數。 使用配置或頁面中的<%@頁面EnableEventValidation =“ true”%>啟用事件驗證。 為了安全起見,此功能驗證回發或回調事件的參數源自最初呈現它們的服務器控件。 如果數據有效且預期,請使用ClientScriptManager.RegisterForEventValidation方法以注冊回發或回調數據以進行驗證。

從未嘗試過准確地執行此處的操作,我的最佳估計是在執行PostBack時ViewState無效,因為您正在使用Ajax填充下拉列表。 看來您正在使用Ajax調用填充下拉列表,然后嘗試使用整頁的帖子將數據發送回服務器。您在這里有幾件事我不太了解,但是我建議這是。

我認為您需要在服務器端執行“請求/發布”,即在請求上綁定服務器的下拉列表和文本框控件,然后使用普通的回發將數據發送回服務器。 或者,兩種方式都使用AJAX-接收並發送數據,但不要嘗試按原樣混合它們。

嘗試,通過jQuery

$( "#ddlstate" ).change(function() {
  $('[id*=Hiddenfield1]').attr('value', $( "#ddlstate" ).val());
});

身體

<asp:HiddenField ID="Hiddenfield1" runat="server">

xp.Parameters.AddWithValue("@STATE_ID", Hiddenfield1.vlue);

暫無
暫無

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

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