none
DataBindings Not Working RRS feed

  • Question

  • In VS 2012 running under Windows 7 Enterprise I am trying to display data in a dataset on a form. I have bound the controls on the form to the dataTable using databindings. The form was working and showed records in the dataTable, but has since stopped working. In debug mode I can see that the dataTable has records returned from my stored procedure but those records are no longer showing on the form. Not sure what I'm doing wrong. Thank you.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Globalization;
    using System.Diagnostics;
    using System.Collections;
    
    namespace MSP_Outreach
    {
        public partial class MSPOutreach : Form
        {
            SqlDataAdapter da;
            DataTable dataTable;
            DataSet cobNonHorizon;
            CurrencyManager currManager;
            Boolean bolDelete;
            Boolean bolConfirm;
            Boolean bolEdit;
    
            public MSPOutreach()
            {
                InitializeComponent();
            }
    
            private void btnShow_Click(object sender, EventArgs e)
            {
                GetCOBNonHorizonRecsForGrp();
    
                /* Enable objects on form */
                btnConfirm.Enabled = true;
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
                btnSave.Enabled = true;
                btnNext.Enabled = true;
                btnPrevious.Enabled = true;
    
                bolEdit = false;
                bolDelete = false;
                bolConfirm = false;
    
                txtComments.Enabled = true;
    
                //RefreshDataGrid();
    
            }
    
            private void GetCOBNonHorizonRecsForGrp()
            {
                Int32 rowsAffected;
    
           
                string GroupName = cboGroups.Text
    
                //Populate dataTable
                SqlCommand cmd1 = new SqlCommand("Insurer_Select", sqlConnection1);
                cmd1.Parameters.Clear();
                cmd1.Parameters.Add(new SqlParameter("@InsurerGroup", GroupName));
                cmd1.CommandType = CommandType.StoredProcedure;
    
                //cobNonHorizon = new DataSet();
                da = new SqlDataAdapter(cmd1);
                this.cobNonHorizon.Clear();
                da.Fill(cobNonHorizon, "dataTable");
                sqlConnection1.Close();
    
                DataTable tbl = cobNonHorizon.Tables[0];
    
                txtPosition.Text = "1 of " + tbl.Rows.Count.ToString();
    
    
                currManager = (CurrencyManager)this.BindingContext[cobNonHorizon.Tables["dataTable"]];
    
                /* Enable status selection buttons */
                btnEdit.Enabled = true;
                btnConfirm.Enabled = true;
                btnDelete.Enabled = true;
    
                /* DetermineStatus(); */
    
    
            }
    
            private void MSPOutreach_Load(object sender, EventArgs e)
            {
                cboGroups.Items.AddRange(GetGroups());
    
                //Create new dataTable
                dataTable = new DataTable("dataTable");
    
                /* Add columns to datatable */
                dataTable.Columns.Add("relHICN", typeof(String));
                dataTable.Columns.Add("relCCID", typeof(String));
                dataTable.Columns.Add("relSubscriberFirst", typeof(String));
                dataTable.Columns.Add("relSubscriberLast", typeof(String));
                dataTable.Columns.Add("relRelation", typeof(String));
                dataTable.Columns.Add("relMSPEffDate", typeof(String));
                dataTable.Columns.Add("relMSPTermDate", typeof(String));
                dataTable.Columns.Add("relMSPCode", typeof(String));
                dataTable.Columns.Add("relInsurerName", typeof(String));
                dataTable.Columns.Add("relInsurerAddress1", typeof(String));
                dataTable.Columns.Add("relInsurerCity", typeof(String));
                dataTable.Columns.Add("relInsurerSt", typeof(String));
                dataTable.Columns.Add("relInsurerZip", typeof(String));
                dataTable.Columns.Add("relPolicyNumber", typeof(String));
                dataTable.Columns.Add("relInsurerType", typeof(String));
                dataTable.Columns.Add("relSourceCode", typeof(String));
                dataTable.Columns.Add("relInsGroup", typeof(String));
                dataTable.Columns.Add("relInsurerGroupName", typeof(String));
                dataTable.Columns.Add("relOccuranceStatus", typeof(String));
                dataTable.Columns.Add("relActSource", typeof(String));
                dataTable.Columns.Add("relActMSPEffDate", typeof(String));
                dataTable.Columns.Add("relActMSPtermDate", typeof(String));
                dataTable.Columns.Add("relActMSPStatus", typeof(String));
                dataTable.Columns.Add("relIKAEffDate", typeof(String));
                dataTable.Columns.Add("relIKATermDate", typeof(String));
                dataTable.Columns.Add("relActInsurerName", typeof(String));
                dataTable.Columns.Add("relActRelation", typeof(String));
                dataTable.Columns.Add("relActInsurerAddress1", typeof(String));
                dataTable.Columns.Add("relActMSPCode", typeof(String));
                dataTable.Columns.Add("relActPolicyNumber", typeof(String));
                dataTable.Columns.Add("relActInsGroup", typeof(String));
                dataTable.Columns.Add("relActInsurerGroupName", typeof(String));
                dataTable.Columns.Add("relActInsurerType", typeof(String));
                dataTable.Columns.Add("relComment", typeof(String));
                dataTable.Columns.Add("relTransActionCd", typeof(String));
     
                /* Set data bindings for dataTable */
                txtHICN.DataBindings.Add("Text", dataTable, "relHICN");
                txtCCID.DataBindings.Add("Text", dataTable, "relCCID");
                txtFN.DataBindings.Add("Text", dataTable, "relSubscriberFirst");
                txtLN.DataBindings.Add("Text", dataTable, "relSubscriberLast");
                txtPatRel.DataBindings.Add("Text", dataTable, "relRelation");
                txtMSPEffDt.DataBindings.Add("Text", dataTable, "relMSPEffDate");
                txtMSPTermDt.DataBindings.Add("Text", dataTable, "relMSPTermDate");
                txtMSPCode.DataBindings.Add("Text", dataTable, "relMSPCode");
                txtInsurerName.DataBindings.Add("Text", dataTable, "relInsurerName");
                txtInsurerAddress.DataBindings.Add("Text", dataTable, "relInsurerAddress1");
                txtInsurerCity.DataBindings.Add("Text", dataTable, "relInsurerCity");
                txtInsurerSt.DataBindings.Add("Text", dataTable, "relInsurerSt");
                txtInsurerZip.DataBindings.Add("Text", dataTable, "relInsurerZip");
                txtPolicyNum.DataBindings.Add("Text", dataTable, "relPolicyNumber");
                txtInsurerType.DataBindings.Add("Text", dataTable, "relInsurerType");
                txtSourceCode.DataBindings.Add("Text", dataTable, "relSourceCode");
                txtInsurerGrp.DataBindings.Add("Text", dataTable, "relInsGroup");
                txtInsurerGrpName.DataBindings.Add("Text", dataTable, "relInsurerGroupName");
                txtStatus.DataBindings.Add("Text", dataTable, "relOccuranceStatus");
                txtSourceCd.DataBindings.Add("Text", dataTable, "relActSource");
                txtActEffDt.DataBindings.Add("Text", dataTable, "relActMSPEffDate");
                txtActTermDt.DataBindings.Add("Text", dataTable, "relActMSPtermDate");
                txtMSPStatus.DataBindings.Add("Text", dataTable, "relActMSPStatus");
                txtIKAEff.DataBindings.Add("Text", dataTable, "relIKAEffDate");
                txtIKATerm.DataBindings.Add("Text", dataTable, "relIKATermDate");
                txtActInsurerName.DataBindings.Add("Text", dataTable, "relActInsurerName");
                txtActPatientRel.DataBindings.Add("Text", dataTable, "relActRelation");
                txtActInsurerAddress1.DataBindings.Add("Text", dataTable, "relActInsurerAddress1");
                txtActMSPCode.DataBindings.Add("Text", dataTable, "relActMSPCode");
                txtActPolicyNum.DataBindings.Add("Text", dataTable, "relActPolicyNumber");
                txtActInsurerGrp.DataBindings.Add("Text", dataTable, "relActInsGroup");
                txtActInsurerGrpName.DataBindings.Add("Text", dataTable, "relActInsurerGroupName");
                txtActInsurerType.DataBindings.Add("Text", dataTable, "relActInsurerType");
                txtComments.DataBindings.Add("Text", dataTable, "relComment");
                txtTransActionCd.DataBindings.Add("Text", dataTable, "relTransActionCd");
     
                //DataTable dtIKASpans = new DataTable();
                cobNonHorizon = new DataSet();
            }
    
            public string[] GetGroups()
            {
                var groups = new List<string>();
                sqlConnection1.Open();
                SqlCommand cmd = new SqlCommand("GetInsurerGroups", sqlConnection1);
                cmd.CommandType = CommandType.StoredProcedure;
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    groups.Add(reader.GetString(0));
                }
    
                sqlConnection1.Close();
    
                return groups.ToArray();
            }
        }
    }

    Monday, January 29, 2018 4:53 PM

All replies

  • By looking at your code without being able to run it, I have to wonder why you create a DataTable then add columns then do data binding. Instead when reading data from a stored procedure the results should come back via a DataTable e.g. datatable.Load(cmd.ExecuteReader()) which populates the Datatable with data and of course columns.

    Also wonder why you are creating a new data set in the load event


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 29, 2018 5:32 PM
    Moderator
  • I would at least remove the dataset from your code and simply use the datatable direct. 

    For instance 

    //da.Fill(cobNonHorizon, "dataTable");
    da.Fill(dataTable);


    Success Cor

    Monday, January 29, 2018 6:24 PM