Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Value from llistview

Status
Not open for further replies.

dodge20

MIS
Jan 15, 2003
1,048
US
I am trying to get value from listview to insert into a database. However I can't seem to grab the value. It says subitems is not a member of system.web.ui.webcontrols.listviewitem.

Here is what I am attempting.

Code:
For Each lvItem As ListViewItem In ListView1.Items
 
 
                strSQL = "INSERT INTO table (col1, col2) Values (" & _
                "'" & lvItem.SubItems(1).Text & "', " & _
                "'" & lvItem.SubItems(2).Text & "' )" 

 
        Next

Dodge20
 
Not sure where you got that code from, but have you checked the help files, or on-line? SubItems is not a valid property of a listview item.

I have not used the listviewitem, but in my short time researching, I have found that you will have to use the itemcreated event or loop throug as you are doing and use the findcontrol() method to find the control you want and then get it's value.

If you post the HTML of the control you have, I might be able to come up with an example.
 
The ultimate goal is to insert the data from an excel file into the listview. Then map the columns using a dropdownlist, then generate a sql statement to insert into a sql table. (I know the values in the dropdown don't make sense - i just filled it with data for testing).

I got the above code from searching the web, but obviously it doesn't work. I am open to any suggestions you may have.

Code:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="OrderID" 
        DataSourceID="SqlDataSource1">
<LayoutTemplate>  
                <table id="itemPlaceholderContainer">  
                    <tr runat="server" id="itemPlaceholder">
   <th>Subject</th>
   <td colspan=3/>
                    </tr>  
                </table>  
            </LayoutTemplate> 
        <ItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
                </td>
                <td>
                    <asp:Label ID="CustomerIDLabel" runat="server" 
                        Text='<%# Eval("CustomerID") %>' />
                </td>
                <td>
                    <asp:Label ID="EmployeeIDLabel" runat="server" 
                        Text='<%# Eval("EmployeeID") %>' />
                </td>
                <td>
                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
                </td>
                <td>
                    <asp:Label ID="RequiredDateLabel" runat="server" 
                        Text='<%# Eval("RequiredDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShippedDateLabel" runat="server" 
                        Text='<%# Eval("ShippedDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipViaLabel" runat="server" Text='<%# Eval("ShipVia") %>' />
                </td>
                <td>
                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipAddressLabel" runat="server" 
                        Text='<%# Eval("ShipAddress") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
                </td>
                <td>
                    <asp:Label ID="CustomerIDLabel" runat="server" 
                        Text='<%# Eval("CustomerID") %>' />
                </td>
                <td>
                    <asp:Label ID="EmployeeIDLabel" runat="server" 
                        Text='<%# Eval("EmployeeID") %>' />
                </td>
                <td>
                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
                </td>
                <td>
                    <asp:Label ID="RequiredDateLabel" runat="server" 
                        Text='<%# Eval("RequiredDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShippedDateLabel" runat="server" 
                        Text='<%# Eval("ShippedDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipViaLabel" runat="server" Text='<%# Eval("ShipVia") %>' />
                </td>
                <td>
                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipAddressLabel" runat="server" 
                        Text='<%# Eval("ShipAddress") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="">
                <tr>
                    <td>
                        No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Clear" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="CustomerIDTextBox" runat="server" 
                        Text='<%# Bind("CustomerID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="EmployeeIDTextBox" runat="server" 
                        Text='<%# Bind("EmployeeID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="OrderDateTextBox" runat="server" 
                        Text='<%# Bind("OrderDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="RequiredDateTextBox" runat="server" 
                        Text='<%# Bind("RequiredDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShippedDateTextBox" runat="server" 
                        Text='<%# Bind("ShippedDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipViaTextBox" runat="server" Text='<%# Bind("ShipVia") %>' />
                </td>
                <td>
                    <asp:TextBox ID="FreightTextBox" runat="server" Text='<%# Bind("Freight") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipNameTextBox" runat="server" 
                        Text='<%# Bind("ShipName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipAddressTextBox" runat="server" 
                        Text='<%# Bind("ShipAddress") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipCityTextBox" runat="server" 
                        Text='<%# Bind("ShipCity") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">
                                        <select name='URL1' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                       <select name='URL2' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                            <select name='URL3' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                        <select name='URL4' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                    <select name='URL5' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                    <select name='URL6' onchange='update_selection(this);'>
     <option value=''>Select a Country</option>
      <option value='North America'>North America</option>
      <option value='South America'>South America</option>
      <option value='Africa'>Africa</option>
      <option value='Europe'>Europe</option>
      <option value='Asia'>Asia</option>
      <option value='Australia'>Australia</option>
    </select></th>
                                <th runat="server">
                                    ShipVia</th>
                                <th runat="server">
                                    Freight</th>
                                <th runat="server">
                                    ShipName</th>
                                <th runat="server">
                                    ShipAddress</th>
                                <th runat="server">
                                    ShipCity</th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="">
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <EditItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />
                </td>
                <td>
                    <asp:Label ID="OrderIDLabel1" runat="server" Text='<%# Eval("OrderID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CustomerIDTextBox" runat="server" 
                        Text='<%# Bind("CustomerID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="EmployeeIDTextBox" runat="server" 
                        Text='<%# Bind("EmployeeID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="OrderDateTextBox" runat="server" 
                        Text='<%# Bind("OrderDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="RequiredDateTextBox" runat="server" 
                        Text='<%# Bind("RequiredDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShippedDateTextBox" runat="server" 
                        Text='<%# Bind("ShippedDate") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipViaTextBox" runat="server" Text='<%# Bind("ShipVia") %>' />
                </td>
                <td>
                    <asp:TextBox ID="FreightTextBox" runat="server" Text='<%# Bind("Freight") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipNameTextBox" runat="server" 
                        Text='<%# Bind("ShipName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipAddressTextBox" runat="server" 
                        Text='<%# Bind("ShipAddress") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ShipCityTextBox" runat="server" 
                        Text='<%# Bind("ShipCity") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <SelectedItemTemplate>
            <tr style="">
                <td>
                    <asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>' />
                </td>
                <td>
                    <asp:Label ID="CustomerIDLabel" runat="server" 
                        Text='<%# Eval("CustomerID") %>' />
                </td>
                <td>
                    <asp:Label ID="EmployeeIDLabel" runat="server" 
                        Text='<%# Eval("EmployeeID") %>' />
                </td>
                <td>
                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Eval("OrderDate") %>' />
                </td>
                <td>
                    <asp:Label ID="RequiredDateLabel" runat="server" 
                        Text='<%# Eval("RequiredDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShippedDateLabel" runat="server" 
                        Text='<%# Eval("ShippedDate") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipViaLabel" runat="server" Text='<%# Eval("ShipVia") %>' />
                </td>
                <td>
                    <asp:Label ID="FreightLabel" runat="server" Text='<%# Eval("Freight") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipNameLabel" runat="server" Text='<%# Eval("ShipName") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipAddressLabel" runat="server" 
                        Text='<%# Eval("ShipAddress") %>' />
                </td>
                <td>
                    <asp:Label ID="ShipCityLabel" runat="server" Text='<%# Eval("ShipCity") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>

Dodge20
 
SubItems is not a property of ListBox. it may be the code you previous found was for the WinForms.ListBox. not the WebForms.ListBox. there is a big difference.

if you are using a for each loop there is not need to call the indexer, because you already have the item. you just need to check whether the item is selected or not and then process.
Code:
foreach(var lvItem in ListView1.Items)
{
   if(lvItem.Selected == false) continue;

   using(var command = connection.CreateCommand())
   {
      command.CommandText = "INSERT INTO table (col1, col2) Values (@a, @b)";

      var a = command.CreateParameter();
      a.Name = "a";
      a.Value = lvItem.Value;
      command.Parameters.Add(a);

      var b = command.CreateParameter();
      b.Name = "b";
      b.Value = lvItem.Text;
      command.Parameters.Add(b);

      command.ExecuteNonQuery();
   }
}
there is still alot to be desired from this though. for instance you can batch the sql statements into a single command and reduce the number of remote calls to the database. wrapping the operation in a transaction will also be beneficial.

for more tips on connection management see the FAQ in my signature below.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top