关于 c#:Inserting DropDownList item into SQL Database

Inserting DropDownList item into SQL Database

我有一个 DropDownList,其中填充了来自 SQL 表的数据。在网络表单中,当用户从该列表中选择一个项目时,我希望它将所选的选项插入另一个 SQL 表中,除了 DropDownLists

之外的所有内容都有效

我试过了:

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

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
67
68
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();


你必须添加 not isPostBack 因为每次调用它的函数都会重新加载整个网页,所以每次你可以看到第一个数据被选中。

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

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

1
2
3
4
5
6
7
8
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                //Your Code
            }

        }


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/269645.html

(0)
上一篇 2022年6月22日
下一篇 2022年6月22日

相关推荐

发表回复

登录后才能评论