1

I want to add a Serial No. on the first column in the MySQL table in my C# program. To do this automatically with every row entry, I need to count previous rows and '+1' with the result. How to count numbers of rows present in a column in MySQL with C#?

This is the code I am using now:

private void button2_Click(object sender, EventArgs e)
    {
        string connection = "datasource=localhost;port=3306;username=root;password=root";
        string Query = "insert into database.accounts (idaccounts,Name,EmailID,UserID,Password,DOB,Gender) values('" +3+ "','" + this.AddUserName.Text + "','" + this.AddEmail.Text + "','" + this.AddUserID.Text + "','" + this.AddPassword.Text + "','"+this.dateTimePicker.Text + "','" + Gender + "') ;";
        MySqlConnection conData = new MySqlConnection(connection);
        MySqlCommand cmdData = new MySqlCommand(Query, conData);
        MySqlDataReader myReader;
        try
        {
            conData.Open();
            myReader = cmdData.ExecuteReader();
            MessageBox.Show("User Added Successfully!");
            while(myReader.Read())
            {
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

I want to add the Serial No. in the idaccounts column automatically with every new entry.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bishwajyoti Roy
  • 1,091
  • 1
  • 14
  • 19

0 Answers0