You must Sign In to post a response.
  • Category: Windows

    List box

    hi

    i want to save multi selected values from ListBox to Database in windows application.can any one help me
  • #530596
    Hello narasireddy ,

    I am not getting your question exactly.

    However I am trying to repeat your question in my words. You want to save some selected values of single ListBox in database together.

    But may I know what is the scenario or requirement for saving such.For example, if you want to add months in ListBox to Database you can go through steps:

    (1) Connect to database and table.
    (2) Add values in rows of table with loop.

    So please let me know the requirement so that I can help you better.

    Regards,
    Vikram Singh Saini

    RESULTS MATTER, REASONS NOT!

  • #530773
    try this Code:


    for (int i = 0; i < this.listBox1.SelectedItems.Count; i++)
    {
    SqlConnection cn = new SqlConnection("server=INFO-PROJECT1\\SQLEXPRESS;database=nss;integrated security=true");

    cn.Open();
    SqlCommand cmd = new SqlCommand("insertemp", cn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "insertemp";

    cmd.Parameters.AddWithValue("@emp1",listBox1.SelectedItems[i].ToString());

    cmd.ExecuteNonQuery();

    }

  • #532546
    Hi,
    Try this code..
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    con.Open()

    For i As Integer = 0 To ListBox1.SelectedItems.Count - 1
    Dim j As String
    j = ListBox1.SelectedItems(i).ToString
    query = "insert into employee(emp_id)values('" & Trim(j) & "')"
    cmd = New SqlCommand(query, con)
    cmd.ExecuteNonQuery()
    MsgBox("Items Saved", MsgBoxStyle.Information)
    Next
    End Sub


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.