Solved

NEED ASSISTANCE WITH DDL & TEXTBOX ASSOCIATE & INSERTION

Posted on 2014-10-14
4
154 Views
Last Modified: 2014-10-14
Please review the attached code.    I have a DDL "CondBY" that when a selection is made it auto populates a TEXTBOX "EmpAP" with associated employee numbers from "EmpData".   When viewing the form on the local host when the name is selected both the name and employee numbers are rendered correctly; however, when submitted both table columns:   CondBY and EmpAP are filled with the employee number.   I need assistance with the code to get the "CondBY" selected name in as well.


cmd.Parameters.Add("@Dates", System.Data.SqlDbType.DateTime, 8).Value = DateTime.Now;
        cmd.Parameters.Add("@Station1", System.Data.SqlDbType.NVarChar, 50).Value = Station1.SelectedValue;
        cmd.Parameters.Add("@Ship1", System.Data.SqlDbType.NVarChar, 50).Value = Ship1.SelectedValue;
        cmd.Parameters.Add("@CondBy", System.Data.SqlDbType.NVarChar, 50).Value = CondBY.SelectedValue;
        cmd.Parameters.Add("@EmpAP", System.Data.SqlDbType.NVarChar, 50).Value = EmpAP.Text;
        cmd.Parameters.Add("@EmpAP1", System.Data.SqlDbType.NVarChar, 50).Value = EmpAP1.Text;
        cmd.Parameters.Add("@Doc1", System.Data.SqlDbType.NVarChar, 50).Value = Doc1.Text;
        cmd.Parameters.Add("@Doc2", System.Data.SqlDbType.NVarChar, 50).Value = Doc2.Text;
        cmd.Parameters.Add("@Discussion", System.Data.SqlDbType.NVarChar, 4000).Value = Discussion.Text;
        cmd.Parameters.Add("@InspBy1", System.Data.SqlDbType.NVarChar, 50).Value = InspBy1.SelectedValue;
        cmd.Parameters.Add("@Conmx", System.Data.SqlDbType.NVarChar, 50).Value = Conmx.Text;
        cmd.Parameters.Add("@APno", System.Data.SqlDbType.NVarChar, 50).Value = APno.Text;

        cmd.ExecuteNonQuery();
        con.Close();

Open in new window


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillDropdown();
        }
    }
    //Filldrop down
    void FillDropdown()
    {
        SqlConnection con = new SqlConnection("");// connection string;
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from EmpData", con);
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        CondBY.DataSource = ds;
        CondBY.DataTextField = "Name1";//Database table filed name for display in dropdown
        CondBY.DataValueField = "EmpNo";//display in textbox when select dropdown field
        CondBY.DataBind();

    }

    protected void CondBY_SelectedIndexChanged(object sender, EventArgs e)
    {
        EmpAP.Text = CondBY.SelectedValue.ToString();// get value form dropdown


    }

}

Open in new window

0
Comment
Question by:BOEING39
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40380622
Shouldn't this line
cmd.Parameters.Add("@CondBy", System.Data.SqlDbType.NVarChar, 50).Value = CondBY.SelectedValue;

Open in new window

be
cmd.Parameters.Add("@CondBy", System.Data.SqlDbType.NVarChar, 50).Value = CondBY.SelectedItem.Text;

Open in new window

if you want the displayed text in your dropdown list?
0
 

Author Comment

by:BOEING39
ID: 40380792
The text is displaying both in the DDL /TextBox properly; however, it is just not being entered into the table correctly.    It is entering the employee number into both the name and employee number positions.   As I explained in the initial problem I have a DDL "CondBY" and a TextBox "EmpAP".   When the DDL name is selected the corresponding employee number fills the TextBox "EmpAP".   This all is rendering properly. However; when a submit occurs the employee number populates both the data columns "CondBY" and "EmpAP" data columns.   The employee name selected isn't going into the table.
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40380826
Sorry, I typed that last part and didn't catch my error. If you want the employee name saved to the database you need to use the SelectedItem's text property, as shown in the provided code. The code you provided is using the selected value when saving to the database, and when setting the EmpAP textbox in the DDL's SelectedIndexChanged event.
0
 

Author Closing Comment

by:BOEING39
ID: 40380926
Thx that took care of it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question