Adding from a Listbox to database

98 views Asked by At

I have a text box where I introduce a number and a button named Add which introduce the number in the list box. After adding all the numbers wanted when I click on a button Finalize to add what is in the list box in database.

My database table is Number with ID IDnumber NAME.

IDnumber = when I add multiple numbers from list box in database I want that all numbers have the same ID.

For example in my list I have

  • 45341
  • 5466
  • 4646
  • 464

and in my database to have

     ID   IDnumber     NAME                  

      1        1        45341                 
      1        1         5466            
      1        1        4646             
      1        1         464

And the next time I will add from list box , the next number will have the IDnumber 2.

    <asp:TextBox ID="txtNrShipment" runat="server" Height="16px"  Width="100px"></asp:TextBox>
<asp:Button ID="btnAddSO" runat="server" Text="Add" OnClick="btnAddSO_Click" CausesValidation="False" />
 <asp:ListBox ID="ListBoxSO" runat="server">               
                </asp:ListBox>


<asp:Button ID="btnFinalizeSO" runat="server" Text="Finalize" OnClick="btnFinalizeSO_Click" />



  protected void btnAddSO_Click(object sender, EventArgs e)
    {
        ListBoxSO.Items.Add(txtNrShipment.Text);
        txtNrShipment.Text = " ";
    }
2

There are 2 answers

1
alejoaldana On BEST ANSWER

You have not provided the actual code of btnFinalizeSO's click event. If you do not have it yet, the first step you must implement is the connection to your database, for example with Entity Framework. If you connect with Entity Framework you could create objects of your table from the database to inject them to the db. For example:

protected void btnFinalizeSO_Click(object sender, EventArgs e)
{
    using(MyDataBaseEntities db = new MyDataBaseEntities())
   {
       Number lastNumber = (from c in db.Number orderby IDNumber select c).LastOrDefaul();
       foreach(var item in ListBoxSO.Items)
       {

            Number n = new Number();
            n.IdNumber = lastNumber.IdNumber +1; 
            n.Id = lastNumber.Id + 1;
            n.Name = ListBoxSO.GetItemText(item);
            db.Number.Add(n);
       }
      db.SaveChanges();
   }
}

This is more or less what you need. May be it is good to catch exceptions.

0
jomsk1e On

You should read and learn basic ADO.NET to do that, simple insert implementation would be something like this (code not tested, you need to use loop to do what you want, but this example is just a basic insert.):

string connectionString = "yourConnectionString";
string query = "INSERT INTO dbo.YourTable (ID, IDnumber, NAME) " + 
               "VALUES (@Id, @IDnumber, @Name) ";

// create connection and command
using(SqlConnection cn = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand(query, cn))
{
    // define parameters and their values
    cmd.Parameters.Add("@ID", ID);
    cmd.Parameters.Add("@IDnumber", IDnumber);
    cmd.Parameters.Add("@Name", Name); 

    //open, insert and close
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}