将DropDownList项目插入SQL数据库

时间:2019-08-19 10:45:41

标签: c# asp.net sql-server webforms

我有一个DropDownList,其中装有SQL表中的数据。在网络表单中,当用户从该列表中选择一个项目时,我希望它将选择的选项插入到另一个SQL表中,除DropDownLists之外的所有功能均正常工作

我已经尝试过:

cmd.Parameters.AddWithValue("@*", ddl*.SelectedValue);
cmd.Parameters.AddWithValue("@*", ddl*.SelectedItem.Text);

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.Windows.Forms;
using System.Data;
using System.Configuration;
using System.Text;
using System.Drawing;

namespace VXUK2
{
    public partial class booking_system : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // SQL Query For DropDownList1 (CIT Company)
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\****;Persist Security Info=True;User ID=***;Password=****;Connect Timeout=30");
                con.Open();
                SqlCommand cmd = new SqlCommand("Select CIT_ID, CIT_CompanyName from CIT_Details", con);
                ddlCITCompany.DataSource = cmd.ExecuteReader();
                ddlCITCompany.DataTextField = "CIT_CompanyName";
                ddlCITCompany.DataValueField = "CIT_ID";
                ddlCITCompany.DataBind();

            // SQL Query for DropDownList2 (Site Details)
                SqlConnection con2 = new SqlConnection();
                con2.ConnectionString = ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\***;Persist Security Info=True;User ID=***;Password=****;Connect Timeout=30");
                con2.Open();
                SqlCommand cmd2 = new SqlCommand("Select Site_ID, Site_Name from Site_Details", con2);
                ddlVisitingCentre.DataSource = cmd2.ExecuteReader();
                ddlVisitingCentre.DataTextField = "Site_Name";
                ddlVisitingCentre.DataValueField = "Site_ID";
                ddlVisitingCentre.DataBind();

        }

        protected void Submit2_Click(object sender, EventArgs e)

        {

            //LocalDB Connection & Execution String
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\***;Persist Security Info=True;User ID=***;Password=****;Connect Timeout=30");
                con.Open();
                String st = "INSERT INTO Booking_Data (Visiting_Centre, Expected_Arrival_Date, Expected_Arrival_Time, Actual_Arrival_Date, Actual_Arrival_Time, CIT_Company, Driver_Name, Vehicle_Registration, Supplied_Password, Delivery_In, Delivery_Out, Time_Booked_In, Time_Booked_Out) values (@Visiting_Centre, @Expected_Arrival_Date, @Expected_Arrival_Time, @Actual_Arrival_Date, @Actual_Arrival_Time, @CIT_Company, @Driver_Name, @Vehicle_Registration, @Supplied_Password, @Delivery_In, @Delivery_Out, @Time_Booked_In, @Time_Booked_Out)";
                SqlCommand cmd = new SqlCommand(st, con);

                cmd.Parameters.AddWithValue("@Visiting_Centre", ddlVisitingCentre.SelectedValue);
                cmd.Parameters.AddWithValue("@Expected_Arrival_Date", txtExpectedArrivalDate.Text);
                cmd.Parameters.AddWithValue("@Expected_Arrival_Time", txtExpectedArrivalTime.Text);
                cmd.Parameters.AddWithValue("@Actual_Arrival_Date", txtActualArrivalDate.Text);
                cmd.Parameters.AddWithValue("@Actual_Arrival_Time", txtActualArrivalTime.Text);
                cmd.Parameters.AddWithValue("@CIT_Company", ddlCITCompany.SelectedValue);
                cmd.Parameters.AddWithValue("@Driver_Name", txtDriverName.Text);
                cmd.Parameters.AddWithValue("@Vehicle_Registration", txtVehicleRegistration.Text);
                cmd.Parameters.AddWithValue("@Supplied_Password", txtSuppliedPassword.Text);
                cmd.Parameters.AddWithValue("@Delivery_In", txtDeliveryIn.Text);
                cmd.Parameters.AddWithValue("@Delivery_Out", txtDeliveryOut.Text);
                cmd.Parameters.AddWithValue("@Time_Booked_In", txtTimeBookedIn.Text);
                cmd.Parameters.AddWithValue("@Time_Booked_Out", txtTimeBookedOut.Text);
                cmd.ExecuteNonQuery();
                con.Close();

1 个答案:

答案 0 :(得分:1)

您不必添加isPostBack,因为每次调用它的函数重新加载整个网页时,因此每次您看到的第一个数据都被选择。

通过添加!IsPostBack,页面不会隐式重新加载数据。

这可以解决您在选择数据时遇到的问题。

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                //Your Code
            }

        }