繁体   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