private void btnIDGenerate_Click(object sender, EventArgs e) { if (cbAutoID.Checked == true) { string szSQL = string.Format("SELECT CUSTID, FIRSTNAME, LASTNAME FROM CUSTOMERS"); SqlConnection conn = DBProcess.DBConnection(); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(szSQL, conn); DataTable dt = new DataTable(); sda.Fill(dt); conn.Close(); var random = new Random(); int randID = random.Next(100000, 999999); List<int> CustIDs = new List<int>(); for (int i = 0; i < dt.Rows.Count; i++) { while (randID == dt.Rows[i].Field<int>(0)) { randID = random.Next(100000, 999999); } } tbID.Text = randID.ToString(); } }
public DataTable GetData() { string szSQL = string.Format("SELECT CUSTID, FIRSTNAME, LASTNAME FROM CUSTOMERS"); SqlConnection conn = DBProcess.DBConnection(); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(szSQL, conn); DataTable dt = new DataTable(); sda.Fill(dt); conn.Close(); dgvCustomers.Rows.Clear(); return(dt); }