none
get value from db into dropdown RRS feed

  • Question

  • hi,

    getting  a value from db into dropdown.If i use datasource  i got only inserted values from  databse into one dropdown .Iwant to show if it is dropdown1 value  show it indropdown1  it there is another value that one from dropdown2 then show it in dropdown2.

    If i use ddlServicesserv1.Items.FindByValue im not getting anything from databse.

    If i use

    ddlServicesserv1.DataSource = dsTimeSheetDetails.Tables[0];
                        ddlServicesserv1.DataTextField = "Services";
                       ddlServicesserv1.DataValueField = "Services";
                        ddlServicesserv1.DataBind();

    if i have two value in databse it shows both values in one dropdown but i want to show one value  in dropdown1 and second value in dropdown2, and want to populate remaining values in both  dropdowns?

     int recordCounter = 1;
                foreach (DataRow dr in dsTimeSheetDetails.Tables[0].Rows)
                {
                    if(recordCounter == 1)
                    {
                       // ddlServicesserv1.Items.FindByValue(dr["Services"].ToString().Trim()).Selected = true;
                        ddlServicesserv1.DataSource = dsTimeSheetDetails.Tables[0];
                        ddlServicesserv1.DataTextField = "Services";
                       ddlServicesserv1.DataValueField = "Services";
                        ddlServicesserv1.DataBind();
                        txt_ServicesVenue.Text = dr["Venue"].ToString().Trim();
                        txt_ServicesStartTIme.Text = dr["StartTime"].ToString().Trim();
                        txt_ServicesEndTime.Text =dr["EndTime"].ToString ().Trim();
                        txt_ServicesBreakfast.Text = dr["Breakfast"].ToString().Trim();
                        txt_ServicesLunch.Text = dr["Lunch"].ToString().Trim();
                        txt_ServicesDinner.Text = dr["Dinner"].ToString().Trim();
                        txt_ServicesHrs.Text = dr["Total"].ToString().Trim();
                        txt_DetTotal.Text = dr["HoursTotal"].ToString().Trim();
                    }  
                    else  if(recordCounter == 2)
                     {
                        // ddlServicesserv2.Items.FindByValue(dr["Services"].ToString().Trim()).Selected = true;
                         ddlServicesserv2.DataSource = dsTimeSheetDetails.Tables[0];
                         ddlServicesserv2.DataTextField = "Services";
                         ddlServicesserv2.DataValueField = "Services";
                         ddlServicesserv2.DataBind();
                        txt_ServicesVenue1.Text = dr["Venue"].ToString().Trim();
                          txt_ServicesStarttime1.Text = dr["StartTime"].ToString().Trim();
                          txt_ServicesEndTime1.Text = dr["EndTime"].ToString().Trim();
                          txt_ServicesBreakfast1.Text = dr["Breakfast"].ToString().Trim();
                          txt_ServicesLunch1.Text = dr["Lunch"].ToString().Trim();
                          txt_ServicesDinner1.Text = dr["Dinner"].ToString().Trim();
                          txt_ServicesHrs1.Text = dr["Total"].ToString().Trim();
                          txt_DetTotal.Text = dr["HoursTotal"].ToString().Trim();
                    }
                    else if(recordCounter == 3)
                    {
                      //  ddlServicesserv3.Items.FindByValue(dr["Services"].ToString().Trim()).Selected = true;
                           txt_ServicesVenue2.Text = dr["Venue"].ToString().Trim();
                           txt_ServicesStarttime2.Text = dr["StartTime"].ToString().Trim();
                           txt_ServicesEndTime2.Text = dr["EndTime"].ToString().Trim();

    Tuesday, January 21, 2014 4:14 PM

Answers

  • Hello,

    >> getting  a value from db into dropdown.If i use datasource  i got only inserted values from  databse into one dropdown .Iwant to show if it is dropdown1 value  show it indropdown1  it there is another value that one from dropdown2 then show it in dropdown2.

    If it is that your DataTable schema is like below:

    Services BelongToWho

    1             Drop1

    2             Drop2

    3             Both

    Then you want dropdown1 show:

    1             Drop1

    3             Both

    Dropdown2 to be:

    2             Drop2

    3             Both

    If it is, we just need to do a filter like below:

    this.DropDownList1.DataSource = dt.AsEnumerable().Where(o => o.Field<string>("BelongToWho") != "Drop2").Select(o => new { Services = o.Field<string>("Services"), BelongToWho= o.Field<string>(" BelongToWho") }).ToList();
    
                    DropDownList1.DataTextField = "Services";
    
                    DropDownList1.DataValueField = "Services";
    
                    DropDownList1.DataBind();
    
    this.DropDownList2.DataSource = dt.AsEnumerable().Where(o => o.Field<string>("BelongToWho") != "Drop1").Select(o => new { Services = o.Field<string>("Services"), BelongToWho= o.Field<string>(" BelongToWho") }).ToList();
    
                    DropDownList2.DataTextField = "Services";
    
                    DropDownList2.DataValueField = "Services";
    
                    DropDownList2.DataBind();
    

    The result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 22, 2014 4:45 AM
    Moderator

All replies

  • Hi,

    I want to get  value form database to dropdown

    ddlServicesserv1.Items.FindByValue(dr["Services"].ToString().Trim()).Selected = true;

    but i didn't get anything its just empty

     

    For below fields i can get values from database

    txt_ServicesVenue.Text = dr["Venue"].ToString().Trim(); txt_ServicesStartTIme.Text = dr["StartTime"].ToString().Trim(); txt_ServicesEndTime.Text =dr["EndTime"].ToString ().Trim(); txt_ServicesBreakfast.Text = dr["Breakfast"].ToString().Trim();


    • Merged by Marvin_Guo Wednesday, January 22, 2014 2:26 AM The same
    Tuesday, January 21, 2014 3:23 PM
  • it seems you have a problem with AdoNet DataSets and not with windows forms.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetdataset

    Your windows forms part is OK


    Success
    Cor

    Tuesday, January 21, 2014 3:59 PM
  • Hemaak,

    You show code somewhere at the end. I know you asked this question in the Windows Forms forum in a shorter way. 

    However you use some code which is typical ASP.Net and that is DataBind 

    Therefore What is:

    1. ddlServicesserv1
    2. What kind of UI are you using (WebForm (ASP), Windows Forms or WPF)
    3. What kind of database do you have
    4. What kind of tech if that is SQL Server (DataSet, Linq to SQL, Linq to Entities or something else?)


    Success
    Cor

    Tuesday, January 21, 2014 4:31 PM
  • Hello,

    >> getting  a value from db into dropdown.If i use datasource  i got only inserted values from  databse into one dropdown .Iwant to show if it is dropdown1 value  show it indropdown1  it there is another value that one from dropdown2 then show it in dropdown2.

    If it is that your DataTable schema is like below:

    Services BelongToWho

    1             Drop1

    2             Drop2

    3             Both

    Then you want dropdown1 show:

    1             Drop1

    3             Both

    Dropdown2 to be:

    2             Drop2

    3             Both

    If it is, we just need to do a filter like below:

    this.DropDownList1.DataSource = dt.AsEnumerable().Where(o => o.Field<string>("BelongToWho") != "Drop2").Select(o => new { Services = o.Field<string>("Services"), BelongToWho= o.Field<string>(" BelongToWho") }).ToList();
    
                    DropDownList1.DataTextField = "Services";
    
                    DropDownList1.DataValueField = "Services";
    
                    DropDownList1.DataBind();
    
    this.DropDownList2.DataSource = dt.AsEnumerable().Where(o => o.Field<string>("BelongToWho") != "Drop1").Select(o => new { Services = o.Field<string>("Services"), BelongToWho= o.Field<string>(" BelongToWho") }).ToList();
    
                    DropDownList2.DataTextField = "Services";
    
                    DropDownList2.DataValueField = "Services";
    
                    DropDownList2.DataBind();
    

    The result:

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 22, 2014 4:45 AM
    Moderator