problem binding comboBox to dataset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Omar

    problem binding comboBox to dataset

    When I try to databind my comboBox (specifically field "emplcode") to a
    filled dataset , the contents of the comboBox displays a bunch of
    "System.Data.Da taRowView". I assume the amount of times
    "System.Data.Da taR..." is displayed inside the combobox is the amount of
    records in the dataset. On the other hand, if my query is "select emplcode
    from payemployee", the databind will work fine (but I don't want to limit
    the dataset to one field). Any help is appreciated. This is the code:

    DataSet DS_Employees = new DataSet();

    SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t; Integrated
    Security=SSPI;" +

    "Initial Catalog=payroll ");

    string strThisQuery = "select * from payemployee"; //If the query were
    "select emplcode from payemployee" it works

    SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);

    DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd parameter?

    SQLConn.Close() ;

    comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];

    comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
    comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";



    Thanks again,

    Omar


  • Kunal

    #2
    Re: problem binding comboBox to dataset

    Set your DataValueField (i.e what the value should be if the combo box
    is taking part in any events) and the DataTextField (what should be
    displayed)..

    try setting

    comboBox_emplco de.DataTextFiel d = " " <-- field u want as the display
    comboBox_emplco de.DataValueFie ld = " " <-- field u want as value..

    also make sure u comboBox_emplco de.DataBind()


    Omar wrote:[color=blue]
    > When I try to databind my comboBox (specifically field "emplcode") to a
    > filled dataset , the contents of the comboBox displays a bunch of
    > "System.Data.Da taRowView". I assume the amount of times
    > "System.Data.Da taR..." is displayed inside the combobox is the amount of
    > records in the dataset. On the other hand, if my query is "select emplcode
    > from payemployee", the databind will work fine (but I don't want to limit
    > the dataset to one field). Any help is appreciated. This is the code:
    >
    > DataSet DS_Employees = new DataSet();
    >
    > SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t; Integrated
    > Security=SSPI;" +
    >
    > "Initial Catalog=payroll ");
    >
    > string strThisQuery = "select * from payemployee"; //If the query were
    > "select emplcode from payemployee" it works
    >
    > SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
    >
    > DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd parameter?
    >
    > SQLConn.Close() ;
    >
    > comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
    >
    > comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
    > comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
    >
    >
    >
    > Thanks again,
    >
    > Omar
    >
    >[/color]

    Comment

    • Jeffrey Tan[MSFT]

      #3
      RE: problem binding comboBox to dataset


      Hello Omar,

      You can do something like this:
      try
      {
      DataSet ds=new DataSet();
      SqlDataAdapter adapter=new SqlDataAdapter( "select * from
      jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
      adapter.Fill(ds );
      comboBox1.DataS ource=ds.Tables[0];
      comboBox1.Displ ayMember="job_d esc";
      comboBox1.Value Member="job_id" ;
      }
      catch(Exception ex)
      {
      MessageBox.Show (ex.Message );
      }

      I use the default database of SqlServer and use the job_desc column as the
      display item of comboBox, job_id column as the value item of the comboBox.
      You can get the current selected member of these 2 column value by
      comboBox1.Selec tedText and comboBox1.Selec tedValue.

      Hope this helps,
      Best regards,
      Jeffrey Tan
      Microsoft Online Partner Support
      Get Secure! - www.microsoft.com/security
      This posting is provided "as is" with no warranties and confers no rights.

      --------------------
      | Reply-To: "Omar" <none>
      | From: "Omar" <none>
      | Subject: problem binding comboBox to dataset
      | Date: Wed, 8 Oct 2003 17:51:35 -0500
      | Lines: 36
      | X-Priority: 3
      | X-MSMail-Priority: Normal
      | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
      | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
      | Message-ID: <uX0LGbejDHA.25 36@tk2msftngp13 .phx.gbl>
      | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      | NNTP-Posting-Host: 66-50-71-153.prtc.net 66.50.71.153
      | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
      | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1900 27
      | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      |
      | When I try to databind my comboBox (specifically field "emplcode") to a
      | filled dataset , the contents of the comboBox displays a bunch of
      | "System.Data.Da taRowView". I assume the amount of times
      | "System.Data.Da taR..." is displayed inside the combobox is the amount of
      | records in the dataset. On the other hand, if my query is "select emplcode
      | from payemployee", the databind will work fine (but I don't want to limit
      | the dataset to one field). Any help is appreciated. This is the code:
      |
      | DataSet DS_Employees = new DataSet();
      |
      | SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
      Integrated
      | Security=SSPI;" +
      |
      | "Initial Catalog=payroll ");
      |
      | string strThisQuery = "select * from payemployee"; //If the query were
      | "select emplcode from payemployee" it works
      |
      | SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
      |
      | DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd parameter?
      |
      | SQLConn.Close() ;
      |
      | comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
      |
      | comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
      | comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
      |
      |
      |
      | Thanks again,
      |
      | Omar
      |
      |
      |

      Comment

      • Omar

        #4
        Re: problem binding comboBox to dataset

        Thanks for the response.
        In my case, the DataTextField or DataValueField members don't exist and the
        DataBind method doesn't exist either.

        "Kunal" <kunal@nospamso removethisqnal. net> wrote in message
        news:uwMEngejDH A.1808@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Set your DataValueField (i.e what the value should be if the combo box
        > is taking part in any events) and the DataTextField (what should be
        > displayed)..
        >
        > try setting
        >
        > comboBox_emplco de.DataTextFiel d = " " <-- field u want as the display
        > comboBox_emplco de.DataValueFie ld = " " <-- field u want as value..
        >
        > also make sure u comboBox_emplco de.DataBind()
        >
        >
        > Omar wrote:[color=green]
        > > When I try to databind my comboBox (specifically field "emplcode") to a
        > > filled dataset , the contents of the comboBox displays a bunch of
        > > "System.Data.Da taRowView". I assume the amount of times
        > > "System.Data.Da taR..." is displayed inside the combobox is the amount of
        > > records in the dataset. On the other hand, if my query is "select[/color][/color]
        emplcode[color=blue][color=green]
        > > from payemployee", the databind will work fine (but I don't want to[/color][/color]
        limit[color=blue][color=green]
        > > the dataset to one field). Any help is appreciated. This is the code:
        > >
        > > DataSet DS_Employees = new DataSet();
        > >
        > > SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;[/color][/color]
        Integrated[color=blue][color=green]
        > > Security=SSPI;" +
        > >
        > > "Initial Catalog=payroll ");
        > >
        > > string strThisQuery = "select * from payemployee"; //If the query were
        > > "select emplcode from payemployee" it works
        > >
        > > SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
        > >
        > > DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd[/color][/color]
        parameter?[color=blue][color=green]
        > >
        > > SQLConn.Close() ;
        > >
        > > comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
        > >
        > > comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
        > > comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
        > >
        > >
        > >
        > > Thanks again,
        > >
        > > Omar
        > >
        > >[/color]
        >[/color]



        Comment

        • Omar

          #5
          Re: problem binding comboBox to dataset

          Thanks for your post. I tried it your way with the same result. Instead of
          displaying 111 emplcodes (the amount of records in the table), it displays
          111 "System.Data.Da taRowView". I *did* notice that it ignores the
          comboBox.Displa yMember member.

          I also tried it like this:
          try
          {
          DataSet DS_Employees=ne w DataSet();
          SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
          Integrated Security=SSPI;" +
          "Initial Catalog=payroll ");
          SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
          DA_empl.Fill(DS _Employees, "payemploye e");
          SQLConn.Close() ;
          DataView dv = new DataView(DS_Emp loyees.Tables["Payemploye e"]);
          comboBox1.DataS ource=dv;
          comboBox1.Displ ayMember = "FName";
          // I can susbstitute the previous line with comboBox1.Displ ayMember =
          "thisFieldDoesn tExist"; and it'll display the same 111
          "System.Data.Da taR..." lines
          catch(Exception ex)
          {
          MessageBox.Show (ex.Message ); //it never displays the MessageBox since
          it never encounters an error.
          }

          "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
          news:jC7LIggjDH A.2556@cpmsftng xa06.phx.gbl...[color=blue]
          >
          > Hello Omar,
          >
          > You can do something like this:
          > try
          > {
          > DataSet ds=new DataSet();
          > SqlDataAdapter adapter=new SqlDataAdapter( "select * from
          > jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
          > adapter.Fill(ds );
          > comboBox1.DataS ource=ds.Tables[0];
          > comboBox1.Displ ayMember="job_d esc";
          > comboBox1.Value Member="job_id" ;
          > }
          > catch(Exception ex)
          > {
          > MessageBox.Show (ex.Message );
          > }
          >
          > I use the default database of SqlServer and use the job_desc column as the
          > display item of comboBox, job_id column as the value item of the comboBox.
          > You can get the current selected member of these 2 column value by
          > comboBox1.Selec tedText and comboBox1.Selec tedValue.
          >
          > Hope this helps,
          > Best regards,
          > Jeffrey Tan
          > Microsoft Online Partner Support
          > Get Secure! - www.microsoft.com/security
          > This posting is provided "as is" with no warranties and confers no rights.
          >
          > --------------------
          > | Reply-To: "Omar" <none>
          > | From: "Omar" <none>
          > | Subject: problem binding comboBox to dataset
          > | Date: Wed, 8 Oct 2003 17:51:35 -0500
          > | Lines: 36
          > | X-Priority: 3
          > | X-MSMail-Priority: Normal
          > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
          > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
          > | Message-ID: <uX0LGbejDHA.25 36@tk2msftngp13 .phx.gbl>
          > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
          > | NNTP-Posting-Host: 66-50-71-153.prtc.net 66.50.71.153
          > | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
          > | Xref: cpmsftngxa06.ph x.gbl[/color]
          microsoft.publi c.dotnet.langua ges.csharp:1900 27[color=blue]
          > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
          > |
          > | When I try to databind my comboBox (specifically field "emplcode") to a
          > | filled dataset , the contents of the comboBox displays a bunch of
          > | "System.Data.Da taRowView". I assume the amount of times
          > | "System.Data.Da taR..." is displayed inside the combobox is the amount of
          > | records in the dataset. On the other hand, if my query is "select[/color]
          emplcode[color=blue]
          > | from payemployee", the databind will work fine (but I don't want to[/color]
          limit[color=blue]
          > | the dataset to one field). Any help is appreciated. This is the code:
          > |
          > | DataSet DS_Employees = new DataSet();
          > |
          > | SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
          > Integrated
          > | Security=SSPI;" +
          > |
          > | "Initial Catalog=payroll ");
          > |
          > | string strThisQuery = "select * from payemployee"; //If the query were
          > | "select emplcode from payemployee" it works
          > |
          > | SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
          > |
          > | DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd[/color]
          parameter?[color=blue]
          > |
          > | SQLConn.Close() ;
          > |
          > | comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
          > |
          > | comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
          > | comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
          > |
          > |
          > |
          > | Thanks again,
          > |
          > | Omar
          > |
          > |
          > |
          >[/color]



          Comment

          • Omar

            #6
            Re: problem binding comboBox to dataset

            I finally found the answer. Apparently, the field in
            comboBox.Displa yMember is case-sensitive (at least for SQL Server 2000).
            The funny thing is that the sql statement used to fill the dataset is *not*
            case-sensitive.

            Thanks again for the help.


            "Omar" <none> wrote in message news:uX0LGbejDH A.2536@tk2msftn gp13.phx.gbl...[color=blue]
            > When I try to databind my comboBox (specifically field "emplcode") to a
            > filled dataset , the contents of the comboBox displays a bunch of
            > "System.Data.Da taRowView". I assume the amount of times
            > "System.Data.Da taR..." is displayed inside the combobox is the amount of
            > records in the dataset. On the other hand, if my query is "select emplcode
            > from payemployee", the databind will work fine (but I don't want to limit
            > the dataset to one field). Any help is appreciated. This is the code:
            >
            > DataSet DS_Employees = new DataSet();
            >
            > SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;[/color]
            Integrated[color=blue]
            > Security=SSPI;" +
            >
            > "Initial Catalog=payroll ");
            >
            > string strThisQuery = "select * from payemployee"; //If the query were
            > "select emplcode from payemployee" it works
            >
            > SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
            >
            > DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd parameter?
            >
            > SQLConn.Close() ;
            >
            > comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
            >
            > comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
            > comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
            >
            >
            >
            > Thanks again,
            >
            > Omar
            >
            >[/color]


            Comment

            • Jeffrey Tan[MSFT]

              #7
              Re: problem binding comboBox to dataset


              Hi Omar,

              I think the SQL statement is passed to the SqlServer by the .Net
              application, and it will be handled by the SqlServer, so it is not
              case-sensitive.
              While binding the ComboBox's DisplayMember property with certain field of
              database, this field is already the column of DataSet and it is processed
              in .Net, so it is case-sensitive.(I think this is determined by the design
              of DisplayMember property).

              I am glad you finally found the problem, I think we all must be careful of
              this type of problem.

              Thanks

              Best regards,
              Jeffrey Tan
              Microsoft Online Partner Support
              Get Secure! - www.microsoft.com/security
              This posting is provided "as is" with no warranties and confers no rights.

              --------------------
              | Reply-To: "Omar" <none>
              | From: "Omar" <none>
              | References: <uX0LGbejDHA.25 36@tk2msftngp13 .phx.gbl>
              | Subject: Re: problem binding comboBox to dataset
              | Date: Thu, 9 Oct 2003 12:16:35 -0500
              | Lines: 48
              | X-Priority: 3
              | X-MSMail-Priority: Normal
              | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
              | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
              | Message-ID: <#UmDjEojDHA.25 28@TK2MSFTNGP12 .phx.gbl>
              | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
              | NNTP-Posting-Host: 66-50-71-48.prtc.net 66.50.71.48
              | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
              | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1902 86
              | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
              |
              | I finally found the answer. Apparently, the field in
              | comboBox.Displa yMember is case-sensitive (at least for SQL Server 2000).
              | The funny thing is that the sql statement used to fill the dataset is
              *not*
              | case-sensitive.
              |
              | Thanks again for the help.
              |
              |
              | "Omar" <none> wrote in message
              news:uX0LGbejDH A.2536@tk2msftn gp13.phx.gbl...
              | > When I try to databind my comboBox (specifically field "emplcode") to a
              | > filled dataset , the contents of the comboBox displays a bunch of
              | > "System.Data.Da taRowView". I assume the amount of times
              | > "System.Data.Da taR..." is displayed inside the combobox is the amount of
              | > records in the dataset. On the other hand, if my query is "select
              emplcode
              | > from payemployee", the databind will work fine (but I don't want to
              limit
              | > the dataset to one field). Any help is appreciated. This is the code:
              | >
              | > DataSet DS_Employees = new DataSet();
              | >
              | > SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
              | Integrated
              | > Security=SSPI;" +
              | >
              | > "Initial Catalog=payroll ");
              | >
              | > string strThisQuery = "select * from payemployee"; //If the query were
              | > "select emplcode from payemployee" it works
              | >
              | > SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
              | >
              | > DA_empl.Fill(DS _Employees, "payemploye e"); //Do I need the 2nd
              parameter?
              | >
              | > SQLConn.Close() ;
              | >
              | > comboBox_emplco de.DataSource = DS_Employees.Ta bles["payemploye e"];
              | >
              | > comboBox_emplco de.DisplayMembe r = "emplcode"; //I also tried
              | > comboBox_emplco de.DisplayMembe r = "payemployee.em plcode";
              | >
              | >
              | >
              | > Thanks again,
              | >
              | > Omar
              | >
              | >
              |
              |
              |

              Comment

              Working...