Create the AutoGenerated Sequential ID in C#

In this article, we will fetch the last entered ID say CustomerID from the CustomerDetails table of the MS Access Database and increment it by 1. We can use this ID anywhere in our application say to use it in the new record etc.

NOTE: If you are using SQL Database, you can replace OleDbConnection and OleDbCommand with SQLConnection and SQLCommand respectively.

If there is no record in the table, this code also checks for that null condition and start the ID from a particular value say 100. We are returning the ID for it to be used at other places in our application.

public static string getCutomerID()
{
     string strCmd = "SELECT MAX(CustomerID) FROM CustomerDetails";
     string custID;
     using(OleDbConnection conn=new OleDbConnection(connectionString))
     {
            try
            {
                OleDbCommand cmd = new OleDbCommand(strCmd, conn);
                conn.Open();
                object obj = cmd.ExecuteScalar();
                if (obj == DBNull.Value)
                    custID = "100";
                else
                    custID = (Convert.ToInt32(obj) + 1).ToString();
                 return custID;
           }
           catch (Exception ex)
           {
                return null;
           }
      }
}

You can use this function to display CustomerID in a TextBox, txtCustomerID:

txtCustomerID.Text= getCutomerID();

Miscellaneous Code – DotNet

Add Values in ComboBox in C#:

cmbCustomerName.Items.Add("Akanksha");

Get the selected item value from combo Box:

string customerName = cmbCustomerName.GetItemText(cmbCustomerName.SelectedItem);

Show current Date and Time:

lbldate.Text=DateTime.Now.ToString();

Connection String to connect Visual Studio C# Windows Form Application to MS Access .accdb Database:

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\DailyExpenses.accdb";

Check for zero rows in a DataTable:

if(dataTable==null){......}

Fetch DataTable CellValue into a variable:

txtCustomerID.Text = custDt.Rows[0]["CustomerID"].ToString();

Enter only numbers and Backspace in TextBox:

private void txtCustomerPhone_KeyPress(object sender, KeyPressEventArgs e)
{
     e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == 8);
}

Add values from one ListBox to Another ListBox. Remove values from a ListBox.

We will demonstrate how to copy value from one ListBox to another ListBox without deleting the values from the list box. Lets create two listboxes: lstAvailableItems and lstSelectedItems AND two buttons: btnAdd and btnRemove.

Below is the code to add items from lstAvailableItems to lstSelectedItems after clicking the Add button. We can remove the items from lstSelectedItems by clicking Remove button.

Below is the design of the form used for this example.

private void btnAdd_Click(object sender, EventArgs e)
{
     string lstboxItem = lstAvailableItems.GetItemText(lstAvailableItems.SelectedItem);
     lstSelectedItems.Items.Add(lstboxItem);
     lstAvailableItems.GetItemText(lstAvailableItems.SelectedItem);
}

private void btnRemove_Click(object sender, EventArgs e)
{
     lstSelectedItems.Items.Remove(lstSelectedItems.GetItemText(lstSelectedItems.SelectedItem));
}

Connect to .accdb instead of .mdb Access database

We sometimes face issues while connecting to .accdb Access Database instead of .mdb Access Database.

For this, we use different Provider while declaring our Connection String.

string connectionString = @”Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\DailyExpenses.mdb“;

For .mdb, we useĀ Microsoft.Jet.OLEDB.4.0 Provider.

For .accdb, we useĀ Microsoft.ACE.OLEDB.12.0 Provider.

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started