Code Snippet
publicclass Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonAddRow_Click(object sender, EventArgs e)
{
int nextID = this.GetNextID();
// write insert code here.
}
private int GetNextID()
{
string sql = "select max(id) from tab_purchase";
DataTable dt = new DataTable();
SqlConnection sqlConn = new SqlConnection("Your Connection String");
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConn);
try
{
sqlConn.Open();
sqlDataAdapter.Fill(dt);
sqlConn.Close();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
int currentMaxID = Convert.ToInt32(dt.Rows[0][0]);
return currentMaxID + 1;
}
If you have any more question, please feel free to let me know.
Best Regards,
Kira Qian
Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs