Hi Everyone,
Can I ask your favor, I have a code that I customize.
DBConnect.CS //here ismy db connection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Data;
namespace PAIRDevelopment.Classes
{
public class DBConnect
{
public static string csConnect = "uid=root; database=membership; pooling = false; convert zero datetime=True";
public static MySqlConnection csCon= new MySqlConnection(Classes.DBConnect.csConnect);
public MySqlCommand cmdCon = new MySqlCommand();
public MySqlDataReader reader;
public void nonQuery(string cmdText)
{
cmdCon.Connection = csCon;
csCon.Open();
cmdCon.CommandText = cmdText;
cmdCon.ExecuteNonQuery();
cmdCon.Dispose();
csCon.Close();
}
public void OPEN(string cmdtext)
{
cmdCon.Connection = Classes.DBConnect.csCon;
Classes.DBConnect.csCon.Open();
cmdCon.CommandText = cmdtext;
reader = cmdCon.ExecuteReader();
}
public void CLOSE()
{
reader.Close();
cmdCon.Dispose();
Classes.DBConnect.csCon.Close();
}
}
}
Original Code that I Customize:
string sql = "SELECT MAX(certnumber) FROM Membership.tblpair_institutional_membership";
MySqlConnection cond = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
MySqlCommand cmdDatabase = new MySqlCommand(sql, cond);
try
{
//cond.Open();
string pcount = Convert.ToString(cmdDatabase.ExecuteScalar());
if (pcount.Length == 0)
{
cnumber_isnm.Text = "100000000";
}
else
{
int pcount1 = Convert.ToInt32(pcount);
int pcountAdd = pcount1 + 1;
cnumber_isnm.Text = pcountAdd.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//cond.Close();
}
My Customization:
OpenConCls.OPEN("SELECT MAX(certnumber) FROM Membership.tblpair_institutional_membership");
while (OpenConCls.reader.Read())
{
string pcount = Convert.ToString(OpenConCls.ToString());// here is the problem
if (pcount.Length == 0)
{
cnumber_isnm.Text = "10000000";
}
else
{
int pcount1 = Convert.ToInt32(pcount);//// here is the problem
int pcountAdd = pcount1 + 1;
cnumber_isnm.Text = pcountAdd.ToString();
}
OpenConCls.CLOSE();
Purpose: auto number;
I want to minimize the DB calling on my form declaration, that's why I created the seperated class for DB connection.
Please give me the suggestion or Idea what is the code for the auto number?
regards,
Darryl