Solved

NEED ASSISTANCE WITH DDL & TEXTBOX ASSOCIATE & INSERTION

Posted on 2014-10-14
4
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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