Link to home
Start Free TrialLog in
Avatar of onebite2
onebite2

asked on

List box add,edit and delete event

I have 3 buttons add,edit,delete beneath the list box.If a user clicks on add then it should add a new item to the existing list box ,since it is used query through database.When clicked on add it should insert a record into the table and show on the list box.
Similarly edit and delete should work.Edit should update the database and delete should delete the record from database.
Here by I am attaching my sample code.

My code is incomplete and I am sure its wrong.I want some one to correct the code.

Thanks!!!!
private void AddButton_Click(object sender, EventArgs e)
        {
            {
                ApplicationsListBox.SelectedIndex = -1;
               if (ApplicationsListBox.SelectedItem == null)
                  {
                      //string app = ApplicationsListBox.
                      efpData.InsertApplications();
                   //MessageBox.Show("Select a skill to add"); 
                     // ApplicationsListBox.Items.Add();
                         
 
                  }
               else
               {
                   int found;
                   found = ApplicationsListBox.FindStringExact(ApplicationsListBox.SelectedItem.ToString());
                   if (found == 0)
                   {
                       ApplicationsListBox.Items.Add(ApplicationsListBox.SelectedItem); }
                   else { MessageBox.Show("Application already exists"); }
               }
            }
        }ApplicationsListBox.FindStringExact(ApplicationsListBox.SelectedItem.ToString());
                   if (found == 0)
                   {
                       ApplicationsListBox.Items.Add(ApplicationsListBox.SelectedItem); }
                   else { MessageBox.Show("Application already exists"); }
               }
            }
        }
 
 
EFPDATA.CS
 
 
    public List<string> InsertApplications(string app)
    {
 
        List<string> insertApp = new List<string>();
 
        Database _db;
       _db = DatabaseFactory.CreateDatabase("InformixConnection");
       
        string query = "INSERT INTO [efpapplication] (app)VALUES (@app);";
 
 
        using (DbCommand appCommand = _db.GetSqlStringCommand(query))
        {
           
            appCommand.Parameters.Add(new OleDbParameter("@app",app));
            appCommand.CommandType = CommandType.Text;
 
            using (IDataReader rdr = _db.ExecuteReader(appCommand))
            {
                while (rdr.Read())
                {
 
                    insertApp.Add(rdr["app"].ToString());
 
                }
            }
        }
 
        return insertApp;
 
    }

Open in new window

Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Since you have differect buttons for add, edit and delete, you can use the approach i have given below. I'm kinda giving you the pseudo code for those methods.
private void AddButton_Click(object sender, EventArgs e)
{
    {
        // Insert the value to DB
        ReloadData();
    }
}
 
private void UpdateButton_Click(object sender, EventArgs e)
{
    {
        // Get the current selected value from the list box
        // Update the value in the DB
        ReloadData();
    }
}
 
private void DelteButton_Click(object sender, EventArgs e)
{
    {
        // Get the current selected value from the list box
        // Delete if from the DB
        ReloadData();
    }
}
 
private void ReloadData()
{
    // Clear all items from the list box
    // Get the new value set and add them to the list box
}

Open in new window

Avatar of onebite2
onebite2

ASKER

I appreciate ur help jandromeda....

I need to be able to edit an item that has been selected in a listbox. I need to do so with a button click event. But now I need to be able to enter new values in the listbox. How DO I do this any help will be appreciated.
Can user  type values directly into the listbox????

Here by I am sending the sample code I wrote using ur pseudo template.I have an error being thrown when I clear the items in the list box in reload method.
Error:"Items collection cannot be modified when data source property is set"

Thanks!!!!

  private void EditButton_Click(object sender, EventArgs e)
        {
 
            {
                // Get the current selected value from the list box
                string app=ApplicationsListBox.SelectedItem.ToString();
                efpData.updateDataBase(app);
                
                // Update the value in the DB
                ReloadData();
            }
 
 
        }
 
 private void ReloadData()
        {
            // Clear all items from the list box
            ApplicationsListBox.Items.Clear();
            // Get the new value set and add them to the list box
            ApplicationsListBox.DataSource = AppNames;
        }
 
efdata.cs:
 
    public void  updateDataBase(string app)
    {
        Database _db;
        _db = DatabaseFactory.CreateDatabase("InformixConnection");
 
        //TODO: UpdateDB result falsified for testing to avoid destroying our test data
        try
        {
            string spName = "efp_updateefpconfigpriority";
            int result = 1;
            using (DbCommand dbCommand = _db.GetStoredProcCommand(spName))
            {
                dbCommand.Parameters.Add(new OleDbParameter("app",app));
               // dbCommand.Parameters.Add(new OleDbParameter("respdate", DateTime.Now));
                //result = _db.ExecuteNonQuery(dbCommand);
 
                
            }
 
           // return result;
        }
        catch (Exception ex)
        {
           // HandleException(ex);
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial