KLomax
KLomax

Reputation: 3

Inserting DropDownList item into SQL Database

I have a DropDownList which is populated with data from an SQL Table. Within the webform when users select an item from that list, i want it to insert the selected choice into another SQL table, everything works other than the DropDownLists

I've tried :

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

etc

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

Upvotes: 0

Views: 312

Answers (1)

Nikolash Beura
Nikolash Beura

Reputation: 51

You have to add not isPostBack as every time any function called it reloads the Entire web-page, So every time you can see the first data is selected.

By Adding !IsPostBack the Page will not Re-Load Data Implicitly.

This can Solve your Problem in Selecting Data.

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

        }

Upvotes: 1

Related Questions