Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to c# database programming, and am designing a simple form that has comboboxes, which are binded and when I navigate thru records the display member of the combobox do not show sometimes and displays instead the value member any idea why it is happening.

What I have tried:

This is the code am writing:

private void Frm_R_Load(object sender, EventArgs e)
{
   #region Form load
   this.land_NewTableAdapter.Fill(reentitiesDataset.Land_New);
   FillCombos("T_Region", id_RegionComboBox);
   FillCombos("T_district", id_DistrictComboBox);
   #endregion
}

private void FillCombos(String TableName, ComboBox ComboName)
{
    using (SqlConnection conn = new SqlConnection(string.Format(ConnectionString.DatabaseConnection.GetSQLConnString())))
    {
        try
        {
        conn.Open();
        SqlCommand cmdCombo = new SqlCommand("FillCombos", conn);
        cmdCombo.CommandType = CommandType.StoredProcedure;
        cmdCombo.Parameters.Add("@TableName", SqlDbType.VarChar).Value = TableName;

        SqlDataAdapter DaCombo = new SqlDataAdapter(cmdCombo);
        DataSet DsCombo = new DataSet();
        DaCombo.Fill(DsCombo);

        ComboName.DataSource = DsCombo.Tables[0];
        ComboName.DisplayMember = "Descrp";
        ComboName.ValueMember = "Id";

       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

And this is the procedure:

PROCEDURE [dbo].[FillCombos](@TableName as varchar(100))
AS
BEGIN
    Declare @SQL as varchar(1000)
    SET NOCOUNT ON;
    Set @SQL = 'Select Id,Descrp  from ' + @TableName + ' order by 2 '
    exec sp_sqlexec @sql
END
Structure of the tables:

CREATE TABLE [dbo].[T_District]( [Id] [tinyint] IDENTITY(1,1) NOT NULL, [Descrp] [varchar](50) NOT NULL, CONSTRAINT [PK_T_District] PRIMARY KEY CLUSTERED ( [Descrp] ASC )WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[T_Region]( [Id] [bigint] IDENTITY(1,1) NOT NULL, [Descrp] [varchar](255) NOT NULL, [D2] [varchar](255) NULL, CONSTRAINT [PK_T_Region] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Posted
Updated 12-Feb-18 0:58am

1 solution

I am not sure how your code works but I would use binding and WPF (if thats an option for you) - there are tones of articles around this:
Step by Step WPF Data Binding with Comboboxes[^]

... and you'll be running into it with things like UWP anyway...
 
Share this answer
 
Comments
Josephss73 12-Feb-18 9:03am    
Thank you Dirk will check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900