I'm lost. I've been trying to do this for a while now and I can get halfway what I need. But I can't tell if it's working or not, or where my problem is.

I don't want to use the wizard in VS to add a data source, I'd like to do it all programmatic and I would like my users to be able to browse, edit, add and delete data in a SQL database in a similar way that MS Access works.

This is what I have so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Information_Database_Control_Center
{
    public partial class EditorAccounts : Form
    {
        BindingSource bindsrc = new BindingSource();

        public EditorAccounts()
        {
            InitializeComponent();
        }

        #region On Load Events

        private void EditorAccounts_Load(object sender, EventArgs e)
        {
            #region Error Handlers

            Messages errors = new Messages();

            #endregion

            #region Connect to Database

            //get default server type
            string ServerType = Properties.Settings.Default.SQLType.ToString();

            //get our connection string
            ConnectionManager ConnectionString = new ConnectionManager();
            string SQLConnectionString = ConnectionString.ConnectionString(ServerType);
            SqlConnection Connection = new SqlConnection();
            Connection.ConnectionString = SQLConnectionString;

            //try opening the connection
            try
            {
                Connection.Open();
                Connection.Close();
            }
            catch
            {
                //connection failed
                //give error message
                errors.ErrorMessages(004);
            }

            #endregion

            #region Retrieve Data

            try
            {
                SqlCommand query = new SqlCommand("SELECT * FROM accounts", Connection);
                SqlDataAdapter adrQuery = new SqlDataAdapter(query);
                DataSet dsQuery = new DataSet();
                adrQuery.Fill(dsQuery);

                bindsrc.DataSource = dsQuery;

                DataNavigator.BindingSource = bindsrc;

                companycode.DataBindings.Add("Text", bindsrc, "companycode");
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }

            #endregion
        }

        #endregion
    }
}

No errors are given except on the line companycode.DataBindings.Add("Text", bindsrc, "companycode"); and that error is: "Cannot bind to the property or column companycode on the DataSource. Parameter name: dataMember"

All I have on the form is a data navigator and a blank text box. I want the value of the text box to change according to the record.

What am I missing?

Try this,

public partial class EditorAccounts : Form
    {
        BindingSource bindsrc;

        public EditorAccounts()
        {
            InitializeComponent();
        }
        private void EditorAccounts_Load(object sender, EventArgs e)
        {
            Messages errors = new Messages();
 
            //get default server type
            string ServerType = Properties.Settings.Default.SQLType.ToString();

            //get our connection string
            ConnectionManager ConnectionString = new ConnectionManager();
            string SQLConnectionString = ConnectionString.ConnectionString(ServerType);
            SqlConnection Connection = new SqlConnection();
            Connection.ConnectionString = SQLConnectionString;

           SqlCommand query = new SqlCommand("SELECT * FROM accounts", Connection);
           SqlDataAdapter adrQuery = new SqlDataAdapter(query);
          
           DataSet dsQuery = new DataSet();
           adrQuery.Fill(dsQuery,"accounts");

           bindsrc=new BindingSource(dsQuery,"accounts");

           DataNavigator.BindingSource = bindsrc;

           companycode.DataBindings.Add("Text", bindsrc, "companycode");
        }
    }

adatapost,

That works beautifully! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.