Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2004, 06:29 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to NeoDelphi
Default Dropdown list question

Hi guys, just studying C# as my language of choice for .NET, Iam a Delphi developer trying to catch-up with the .NET fever.

Im working in a datagrid and included one Dropdownlist in my edit template so that user will just have to select item from the list instead of typing it in textbox. I binded my dropdownlist to a maintenance table tblCountry it has two fields country_code and country_name.

How can I get the key field (country_code)of the selected item instead of the list item (which is country_name) for insertion in the Customers table.

Actually i have solved the problem by running separate query on tblCountry (using the country_name selected) then passing the key field to a variable, but I want to know if theres any easier way to deal with this coz that's very easy task in delphi's VCL dropdownlist becoz it has property for key field and display field item.

Thanks for your help.

Regards.

heres my code

public void DataGrid1_Update(Object sender, DataGridCommandEventArgs E)
        {
            SqlConnection MyCon = new SqlConnection(ConStr);
//Get Input value of lblContNo from the GridEdit Template
            string strControl_No = ((Label)(E.Item.FindControl("lblContNo"))).Text;
            string strFName=((TextBox) (E.Item.FindControl("txtFName"))).Text;
            string strLName=((TextBox) (E.Item.FindControl("txtLName"))).Text;
            //Get the Selected Value from the ListBox populated via the BindTheCountry() function.
            strCountry= ((DropDownList)E.Item.FindControl("ddlCountry")).S electedItem.ToString();

//find the key field of that selected country string from the table maintenance.

string SqlLookCode="SELECT country_code, Country FROM tblCountry where Country_Name = '"+strCountry+"'";


//execute sqlLookCode
SqlCommand CmdLocCountry = new SqlCommand(SqlLookCode, MyCon);

            SqlDataReader myReader;
            //open connection
            try
            {
                MyCon.Open();
                myReader = CmdLocCountry.ExecuteReader();
                myReader.Read();
                //assigned query result to Country_Code.
                string Country_Code= myReader.GetString(0);
                myReader.Close();
                //Save the country_code value to employee master file.
                string SqlStr= "UPDATE H_EMPMASTER SET First_Name='" + strFName
                    +"',Last_Name='"+strLName+"',Country='"+Country_Co de+"'WHERE Control_No='" + strControl_No + "'";

                SqlCommand SqlCmd = new SqlCommand(SqlStr,MyCon);
                //Execute SqlStr.
                SqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on);
                //Return to normal grid view.
                DataGrid1.EditItemIndex = -1;
                //re-bind grid to updated table source.
                BindGrid();
            }
            catch (SqlException ex)
            {
                lblError.Text = "Error: "+ ex.Message;
            }
            finally
            {
             MyCon.Close();
            }

        }

.....
 
Old December 22nd, 2004, 01:55 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to NeoDelphi
Default

any idea so far? if none then I think i got the best method (the conventional one).

Great Imar any Idea?

Thanks and Regards,

Neo

.....
 
Old December 22nd, 2004, 04:22 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

A DropDownList control exposes a SelectedValue property that gives you the, euh, Selected Value ;)

Alternatively, I think this would work too:

strCountry= ((DropDownList)E.Item.FindControl("ddlCountry")).S electedItem.Value;

That is because the SelectedItem is a ListItem which exposes Text and Value properties among others....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 25th, 2004, 12:46 AM
Registered User
 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to NeoDelphi
Default

thanks man..

best regards,

Neo

Quote:
quote:Originally posted by Imar
 Hi there,

A DropDownList control exposes a SelectedValue property that gives you the, euh, Selected Value ;)

Alternatively, I think this would work too:

strCountry= ((DropDownList)E.Item.FindControl("ddlCountry")).S electedItem.Value;

That is because the SelectedItem is a ListItem which exposes Text and Value properties among others....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
.....





Similar Threads
Thread Thread Starter Forum Replies Last Post
Two Level Dynamic DropDown List Error Question workib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 May 3rd, 2008 02:37 AM
Dropdown list question dcct84 C# 4 October 2nd, 2007 02:48 PM
fill dropdown list with items when parent list isaac_cm Pro PHP 1 July 10th, 2006 05:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.