Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Me again...

On a form i have a ComboBox, i bind the combobox with some data from a database, next the combobox i have a button "insert new data"

which is the better way after in open the form and press the Save button, in my combobox in the main form to be auto selected the new record inserted

I use somthing like this.

private void GetNewId()
        {
            int newID = 0;
            Program.Connection.CommandText = "SELECT MAX(ClientId) AS ClientId FROM DateClientiAmanet";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            newID = Convert.ToInt32(Table.Rows[0]["ClientId"]);
            
            InformatiiDespreClient();
           
            cboNumeClient.SelectedValue = newID.ToString();
        }

Is ok,works,but i search on the internet and the "MAX(ClientId)" is not so good.
thanks.
[EDIT] Code block moved from answer[/EDIT]
Posted
Updated 28-Mar-11 19:02pm
v3
Comments
m@dhu 29-Mar-11 1:03am    
Always use Improve question to update the question.

1 solution

- Insert record
SQL
INSERT INTO tblBlah1 (Col1,Col2,Col3) VALUES (Val1,Val2,Val3);

- Retrieve id
SQL
SELECT SCOPE_IDENTITY()

- Repopulate your combo
- Set selectedvalue to the new id
 
Share this answer
 
Comments
aciobanita 28-Mar-11 20:47pm    
for the moment i'm using access, but i whant a statement to work for MySql, Access and micosoft sql.
is that posibl?
m@dhu 29-Mar-11 1:02am    
Op updated hi question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900