pls help i m new on c# just read it there is error on script it didn't generate next number error showing "use of assigned local variable max" on this line
"maxCommand.Parameters.Add("@acn", SqlDbType.VarChar).Value = max.ToString();"
complete coding
private void contact_edit_Click(object sender, EventArgs e)
{
int max;
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
con.Open();
SqlCommand maxCommand = new SqlCommand();
maxCommand = con.CreateCommand();
maxCommand.CommandText = ("SELECT max(acn) from contacts");
maxCommand.Parameters.Add("@acn", SqlDbType.VarChar).Value = max.ToString();
maxCommand.ExecuteNonQuery();
max = max ++;
acn.Text = max.ToString();
}
how it can be arrange ???
example acn = 2253 \\ in table "contact" coulmn acn (A/c number)
if acn = 2253 \\collected max number from acn
acn + 1 \\ add 1 to acn
acn = 2254 \\this should be generate
The initiate value of max is unknown. Assign an value would fix the problem.
int max = 0;
To find max and the next value, you can try
maxCommand.CommandText = ("SELECT acn FROM contacts ORDER BY acn DESC");
SqlDataReader reader = maxCommand.ExecuteReader();
reader.Read();
max = Convert.ToInt32(reader[0]); // the max value
max++;// the next value
acn.Text = max.ToString();