i have created C# windows form, in here my expectation is to add new employee to the system and after successfully added need to show form again by clear every field and show next employee to add. in my form their is a auto generating employee number so it also should want to increment by one. but now i can add employee to the system and after close the form and again open it then it will show me next employee number without any matter. but that's not what i need
in advance please
thank you
Loading

John BhattPosted Mar 19, 2014, 6:44 AM
Sorry for delayed reply. You can return last inserted value in SQL Server using simple query. As required by you, there is a requirement of simple Increment operator to display next ID.
SQL command using Output keyword.
VulpesPosted Feb 24, 2014, 10:26 AM
You could do so by adding the following static fields to your Form class:
private static bool firstOpening = true;
private static int lastEmpId = 0;
As these are static fields, the previous values will be persisted if you close the form and then open it again:
When you save a record you can then update the lastEmpId field. So in John's code you'd have:
if(successful)
{
lblSuccessMessage.Text = "Record Added Succesfully!";
lastEmpId++; // increment last emp ID saved
// get ready to add next record
ClearFields();
lblEmpId.Text = (lastEmpId + 1).ToString();
txtEmpFName.Focus();
}
When you open the form, you could check whether this is the first opening. If it is, then you can get the number of records in your database and initialize lastEmpId accordingly:
if (firstOpening)
{
// do database query to count records in the table
// set up and open connection and create command object
// ...
cmd.CommandText = "SELECT COUNT(*) FROM Employees";
lastEmpId = (int)cmd.ExecuteScalar();
firstOpening = false;
}
asela nuwanPosted Feb 24, 2014, 9:08 AM
I think you didn't get it clearly i already know what you coded but i just want to add new record again without closing the form then it should be clear all fields and show the next exp id
John BhattPosted Feb 24, 2014, 6:47 AM
You can create a method to clear all fields.
Navigate to backend and write a simple function as below.
void ClearFields()
{
txtEmpFName.Text ="";
txtEmpLName.Text ="";
txtEmpAddress.Text ="";
txtEmpEmail.Text ="";
txtEmpDOB.Text ="";
BindAddressSelectionDropdown()
lblErrorMessage="";
}
And call this method at Button_Click even of submit button only after record submitted successfully.
.......
{
//Code to submit data.
if( //Submit is success)
{
ClearFields();
lblSuccessMessage.Text = "Record Added Succesfully!";
}
}
All the Best.