Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've a dropdownlist that after I make updates and inserts to the entity I would like to refresh to display the newly inserted contact name. Here's my code for inserting the new record:
C#
private void btnCreate_Click(object sender, EventArgs e)
        {
            var nextContact = context.Contacts.OrderBy("it.ContactID DESC").First();
            int nextContactID = nextContact.ContactID + 1;
            string[] name = txtTenant.Text.Split(' ');
            var contact = Contact.CreateContact(nextContactID, name[0].ToString(), name[1].ToString());
            var address = new Address();
            address.Address1 = txtAddress.Text;
            contact.ContactNumber = txtContactNum.Text;
            address.Mortgage = txtMortgage.Text;
            address.RemainingMortgage = txtRemainingMortgage.Text;
            address.Rent = txtRent.Text;
            address.Period = cboPeriod.SelectedIndex;
            address.RentArrears = txtRentArrears.Text;
            address.Rates = txtRates.Text;
            address.RatesUpToDate = chkRates.Checked;
            address.RentArrears = txtRentArrears.Text;
            address.Repairs = txtRepairs.Text;
            //join the new address to the contact
            address.Contact = contact;
            //add new graph to the contact
            context.AddToContacts(contact);
            context.SaveChanges();
        }
Posted
Comments
Tejas Vaishnav 28-Jan-14 6:43am    
where is your code to bind data to your dropdownlist can you post it
pmcm 28-Jan-14 6:48am    
Here it is:
private void QueryContacts()
{
cboContactList.DataSource = context.Contacts.ToList();
cboContactList.DisplayMember = "FirstName";
cboContactList.ValueMember = "ContactID";
cboContactList.Text = "-- Please Select a Contact --";
cboContactList.Invalidate();
}

1 solution

After the insertion call the function QueryContacts()
 
Share this answer
 
Comments
pmcm 28-Jan-14 8:21am    
When I do this it sets the display value of the Dropdownlist to "-- Please Select a Contact --" which is not what I want if I insert a new contact, i'd like that persons name to be the display value. How do i achieve this?
Praveen_P 28-Jan-14 23:56pm    
select the last inserted ContactID from db using IDENT_CURRENT , then set cboContactList.SelectedValue to last inserted id

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