C# 根据用户为dropdownlist选择的值,将该值的id插入表中

C# 根据用户为dropdownlist选择的值,将该值的id插入表中,c#,asp.net,webforms,insert,dropdownbox,C#,Asp.net,Webforms,Insert,Dropdownbox,我有两个表,分别是预约表和医疗中心表,它们使用mcID相互关联。对于我的预约表单,我填充dropdownlist以显示MedicalCenter表的McCenter字段。我想做的是,根据用户在dropdownlist中为McCenter选择的内容,并提交表单,MedicalCenter表的适当mcID将插入到预约表中。等等,如果我选择Silver Cross Family Clinic并提交表单,预约表将插入mcID 4的新记录。因为根据MedicalCenter表,银跨家庭诊所属于MCID4

我有两个表,分别是预约表和医疗中心表,它们使用mcID相互关联。对于我的预约表单,我填充dropdownlist以显示MedicalCenter表的McCenter字段。我想做的是,根据用户在dropdownlist中为McCenter选择的内容,并提交表单,MedicalCenter表的适当mcID将插入到预约表中。等等,如果我选择Silver Cross Family Clinic并提交表单,预约表将插入mcID 4的新记录。因为根据MedicalCenter表,银跨家庭诊所属于MCID4

对于我发布的以下代码。选择McCenter(Hwang&Liang家庭诊所)并单击“注册”按钮后,我得到了(将nvarchar值“Hwang&Liang家庭诊所”转换为数据类型int时转换失败)帮助

预约表

预约和医疗中心表

hutchonoid代码后出现错误

hutchonoid代码2后出现错误

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

public partial class appointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string constr = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ToString(); // connection string
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            //SqlCommand com = new SqlCommand("select * from MEDICALCENTRE", con); // table name 
            SqlCommand com = new SqlCommand("select mcCentre from MEDICALCENTRE", con); // table name 
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);  // fill dataset
            //ddlMedicalCentre.DataTextField = ds.Tables[0].Columns["mcID"].ToString(); // text field name of table dispalyed in dropdown
            //ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcID"].ToString();             // to retrive specific  textfield name 
            ddlMedicalCentre.DataTextField = ds.Tables[0].Columns["mcCentre"].ToString(); // text field name of table dispalyed in dropdown
            ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcCentre"].ToString();             // to retrive specific  textfield name 
            ddlMedicalCentre.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
            ddlMedicalCentre.DataBind();  //binding dropdownlist
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
            {
                try
                {
                    int ID = Convert.ToInt32(Session["ID"].ToString());
                    SqlCommand cmd = new SqlCommand();
                    Guid guid;
                    guid = Guid.NewGuid();
                    String strStatus = "waiting";
                    string sql = "INSERT INTO appointment (aStatus,aDate, aTime, aContact, aHeight, aWeight, patientID, mcID)";
                    sql += "VALUES (@aStatus, @aDate, @aTime, @aContact, @aHeight, @aWeight, @patientID, @mcID)";
                    cmd.Parameters.AddWithValue("@aStatus", strStatus);
                    cmd.Parameters.AddWithValue("@aDate", txtDate.Value);
                    cmd.Parameters.AddWithValue("@aTime", txtTime.Value);
                    cmd.Parameters.AddWithValue("@aContact", txtContact.Value.Trim());
                    cmd.Parameters.AddWithValue("@aHeight", txtHeight.Value.Trim());
                    cmd.Parameters.AddWithValue("@aWeight", txtWeight.Value.Trim());
                    cmd.Parameters.AddWithValue("@patientID", ID);
                    cmd.Parameters.AddWithValue("@mcID", Convert.ToInt16(ddlMedicalCentre.SelectedValue.Trim()));

                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    con.Open();

                    cmd.ExecuteNonQuery();

                   // Session.Add("Username", txtFirstName.Value);
                  //  Session.Add("Password", txtContact.Value);
                  //  FormsAuthentication.SetAuthCookie(txtFirstName.Value, true);
                    Response.Redirect("../index.aspx");

                }

                finally
                {
                    con.Close();
                }
            }
        }
    }
}


您需要将所选值从
字符串
转换为
Int

这将返回一个字符串:

  ddlMedicalCentre.SelectedValue.Trim()
并且该语句需要一个
Int

这将解决以下问题:

cmd.Parameters.AddWithValue("@mcID", Convert.ToInt16(ddlMedicalCentre.SelectedValue.Trim()));
更新

您需要确保下拉列表使用Id作为值,如下所示:

 ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcId"].ToString();
这将文本设置为以下值:

 ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcCentre"].ToString();
您应该让DataTextField保持原样以显示名称

更新2

将此复制到一个新方法中,即

private void BindMedicalDropdowns()
        {

            string constr = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ToString(); // connection string
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            //SqlCommand com = new SqlCommand("select * from MEDICALCENTRE", con); // table name 
            SqlCommand com = new SqlCommand("select mcCentre from MEDICALCENTRE", con); // table name 
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);  // fill dataset
            //ddlMedicalCentre.DataTextField = ds.Tables[0].Columns["mcID"].ToString(); // text field name of table dispalyed in dropdown
            //ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcID"].ToString();             // to retrive specific  textfield name 
            ddlMedicalCentre.DataTextField = ds.Tables[0].Columns["mcCentre"].ToString(); // text field name of table dispalyed in dropdown
            ddlMedicalCentre.DataValueField = ds.Tables[0].Columns["mcCentre"].ToString();             // to retrive specific  textfield name 
            ddlMedicalCentre.DataSource = ds.Tables[0];      //assigning datasource to the dropdownlist
            ddlMedicalCentre.DataBind();  //binding dropdownlist
        }
然后把它搬出房间!IsPostBack子句,因此它也会在回发时重新绑定

protected void Page_Load(object sender, EventArgs e)
{
    BindMedicalDropdowns();
    if (!Page.IsPostBack)
    {

    }   
}
更新3

在第32行,select语句应该是这样的,它缺少
mcID
字段:

select mcId, mcCentre from MEDICALCENTRE

乍一看,我觉得还可以。有错误吗?在选择McCenter(Hwang&Liang Family Clinic)并单击注册按钮后,我得到了(将nvarchar值“Hwang&Liang Family Clinic”转换为数据类型int时,转换失败)。@hutchonoidNow我得到了这个人。输入字符串的格式不正确@执行之前SQL是什么样子的?你能把它添加到你的问题中让我看一下吗?你是说我的更新代码对吗?我编辑了上面的内容,并添加了一个图像。我我现在出去,我稍后会回来检查,谢谢你@我是新来的。每当有人解答我的问题时,我都会勾选答案。但我的问题还没有解决。我想知道为什么我要在它没有解决之前打勾?以及创建新的线程/问题。系统不会警告我重复的吗?我用我的手机回复你,因为我现在出去了,哈哈@哈奇诺伊德诺,问题已经转移了。你最初的问题是关于Int的施法。这个问题已经解决了,现在它已经转移到了其他方面。这个问题已经成为一个移动的目标。