ListBox Control

Hi All

I want to insert the listbox items in the database table using a Stored procedure.

Any help will be useful

Thanks!!!!!!!1



Answer this question

ListBox Control

  • Douglas R

     

    Hi

    Thanks for replying, but I want to do it using a Stored Proc.

    But I am not able to do it, as the data type of var0 and var1 is int and from here it is picking up string, and it just picks up the 1st value from the listbox

    Dim attr0param, attr1param As SqlParameter

    Dim objCmd As New SqlCommand

    objConn.Open()

    objCmd.Connection = objConn

    objCmd.CommandType = CommandType.StoredProcedure

    objCmd.CommandText = "s_Proc"

    Dim i As Integer

    For i= 0 To lbNames.Items.Count - 1

    attr0param = objCmd.Parameters.Add("@var0", SqlDbType.int)

    attr0param.Direction = ParameterDirection.Input

    attr0param.Value = listbox1.Items(i).Value

    attr1param = objCmd.Parameters.Add("@var1", SqlDbType.int)

    attr1param.Direction = ParameterDirection.Input

    attr1param.Value = listbox1.Items(i+ 1).Value

    Next

     

    and StoredProc is

    Create Procedure dbo.s_Proc
     @var0   int=null,
     @var1   int=null,
     As
     Insert vendorAttributes
     (
     CreateDate,
     ModDate,
     CreateUser,
     ModUser,
     var0,
     var1,
     )
    Select 
     attrCreateDate,
     attrModDate,
     attrCreateUser,
     attrModUser,
     @var0,
     @var1,
     from Table1


  • Sean Shanny

    DataSet ds = new DataSet ();

    DataTable dt = ds.Tables.Add ( "Table1" );

    dt.Columns.Add ( "ListValue", typeof ( String ) );

    foreach ( Object obj in listBox1.Items )

    {

    DataRow dr = dt.NewRow ();

    dr["ListValue"] = obj.ToString ();

    }

    SqlConnection connection = new SqlConnection ( @"DataSource=.\sqlexpress; Initial Catelog=Database1; User Id= sa; Password=sa;" );

    SqlDataAdapter da = new SqlDataAdapter ( "Select ListValue From Table1", connection );

    SqlCommandBuilder cb = new SqlCommandBuilder ( da );

    da.Update ( dt );



  • Pete_M

    Are these values you want to put into the DB coming from the DB in the first place Either way though, I'd create a datatable, bind the listbox to it, set teh DisplayMember and ValueMember fields, and then any modifications done to it will be reflected in the dataset. From there, just call Update and make sure you have your params mapped correctly and valid connection/commands.

  • grellsworth


    Use the Convert.ToInt32() method.

  • ListBox Control