Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Passing newly selected Dropdownlist value to SQL database


Message #1 by "Patterson, Stephanie L" <stephanie.l.patterson@i...> on Wed, 29 May 2002 10:53:42 -0700
I am building a datagrid which contains data from a SQL database (product
table).  Some of the data is displayed in text boxes, some are dropdown
lists.  The source for the dropdownlist is another table in the database
(calendar).  Each dropdownlist has the selected item as the value in the
product table.

If the user selects a different value from the dropdownlist, how do I pass
the new value back to the SQL database?

In advance, thanks!
Stephanie
--------------------------
Here is a code snip-it:

Sub DoItemUpdate(objSource As Object, objArgs As DataGridCommandEventArgs)
  ?? not sure how to pass the newly selected dropdownlist values ??
End Sub 'DoItemUpdate

<asp:DataGrid id="DataGrid1" 
  runat="server" 
  AllowSorting="true"
  OnSortCommand="Sort_Grid"
  OnEditCommand="DoItemEdit"
  OnUpdateCommand="DoItemUpdate"
  OnCancelCommand="DoItemCancel"
  AutoGenerateColumns="false" >
  
  <EditItemStyle BackColor="#ffffcc"/>

  <Columns>
    <ASP:EditCommandColumn
      EditText="Edit"
      CancelText="Cancel"
      UpdateText="Update"
      ItemStyle-Wrap="true"
    />
            
    <ASP:BoundColumn 
      DataField="prod_nm"
      HeaderText="Product Name"
      ReadOnly="True"
      SortExpression="prod_nm" />
          
    <ASP:TemplateColumn>
      <EditItemTemplate>
        <table border="0" Cellpadding="0" Cellspacing="4">
        <tr><td>
        <ASP:dropdownlist 
          id="txtpop_plan"
          runat="server" 
          DataSource='<%# CalendarIndex.DefaultView() %>' 
          DataTextField="Intel_WW" 
          SelectedIndex='<%# GetWWIndex(Container.DataItem("pop_plan")) %>'
/>
        </td></tr>
        </table>
      </EditItemTemplate>
    </asp:TemplateColumn>

  </Columns>
</asp:DataGrid>

Message #2 by "Sri Vidya" <svsvidya@i...> on Thu, 30 May 2002 09:14:08 +0530
Hi,

I am pasting the code for a similar example. This example displays a 
combo box with different CATEGORY NAMES. Based on the user selection 
of CATEGORY NAME, it passes the CATEGORYID to the PRODUCTS table and 
fetches the PRODUCTS AVAILABLE UNDER THE SELECTED CATEGORY.

Hope this helps. 

Cheers,
Vidya.

======================================================================

<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Sub Page_Load

If Not IsPostBack Then
	 Dim conNW as SqlConnection
	 Dim cmdSelect as SqlCommand
	 Dim dtrCategories as SqlDataReader
	 
	 conNW = New 
SqlConnection("server=blrgtpinfo2;uid=sa;pwd=;database=NorthWind")
	 cmdSelect = new SqlCommand("Select CategoryId, CategoryName from 
categories", conNW)
	 conNW.Open()
	 dtrCategories = cmdSelect.ExecuteReader()
	 
	 dropCategories.DataSource = dtrCategories
	 dropCategories.DataTextField = "CategoryName"
	 dropCategories.DataValueField = "CategoryID"
	 dropCategories.DataBind()
	 
	 dropCategories.Items.Insert(0, New ListItem("none selected", -1))
	 dtrCategories.Close()
	 conNW.close()
end if
End Sub

sub bindproducts(intcatid as integer)

		dim connw as sqlconnection
		dim cmdselect as sqlcommand
		dim strselect as string
		dim dtrproducts as sqldatareader
		
		connw = new 
sqlconnection("server=blrgtpinfo2;uid=sa;pwd=;database=northwind")
		connw.open()
		
		strselect = "select productname, unitprice from products where 
categoryid=@catid"
		cmdselect = new sqlcommand(strselect, connw)
		cmdselect.parameters.add("@catid", intcatid)
		dtrproducts = cmdselect.executereader()
		
		rptproducts.datasource = dtrproducts
		rptproducts.databind()
		
		dtrproducts.close()
     connw.close()
end sub

sub dropcategories_selectedindexchanged( s as object, e as eventargs)
		dim intcatid as integer
		
		intcatid = dropcategories.selecteditem.value
		if intcatid <> -1 then
			 bindproducts(intcatid)
		end if
end sub

</script>

<html>
<head>
<title> MasterDetail.aspx </title>
</head>
<body>

<form runat="server">

<b> Select category: </b>
<asp:DropDownList id="dropcategories" autopostback="true" 
			onselectedindexchanged="dropcategories_selectedindexchanged"		runat="server" 
/>
									
<p>

<asp:repeater id="rptproducts" enableviewstate="false" runat="server" 
>
<itemtemplate>
<li> <%# Container.DataItem("ProductName") %> - <%# 
String.Format("{0:c}", Container.DataItem("UnitPrice")) %>
</itemtemplate>
</asp:repeater>

</form>
</body>
</html>
=======================================================================







On Wed, 29 May 2002 10:53:42 -0700
  "Patterson, Stephanie L" <stephanie.l.patterson@i...> wrote:
>I am building a datagrid which contains data from a SQL database 
>(product
>table).  Some of the data is displayed in text boxes, some are 
>dropdown
>lists.  The source for the dropdownlist is another table in the 
>database
>(calendar).  Each dropdownlist has the selected item as the value in 
>the
>product table.
>
>If the user selects a different value from the dropdownlist, how do I 
>pass
>the new value back to the SQL database?
>
>In advance, thanks!
>Stephanie
>--------------------------
>Here is a code snip-it:
>
>Sub DoItemUpdate(objSource As Object, objArgs As 
>DataGridCommandEventArgs)
>   ?? not sure how to pass the newly selected dropdownlist values ??
>End Sub 'DoItemUpdate
>
><asp:DataGrid id="DataGrid1" 
>   runat="server" 
>   AllowSorting="true"
>   OnSortCommand="Sort_Grid"
>   OnEditCommand="DoItemEdit"
>   OnUpdateCommand="DoItemUpdate"
>   OnCancelCommand="DoItemCancel"
>   AutoGenerateColumns="false" >
>   
>   <EditItemStyle BackColor="#ffffcc"/>
>
>   <Columns>
>     <ASP:EditCommandColumn
>       EditText="Edit"
>       CancelText="Cancel"
>       UpdateText="Update"
>       ItemStyle-Wrap="true"
>     />
>             
>     <ASP:BoundColumn 
>       DataField="prod_nm"
>       HeaderText="Product Name"
>       ReadOnly="True"
>       SortExpression="prod_nm" />
>           
>     <ASP:TemplateColumn>
>       <EditItemTemplate>
>         <table border="0" Cellpadding="0" Cellspacing="4">
>         <tr><td>
>         <ASP:dropdownlist 
>           id="txtpop_plan"
>           runat="server" 
>           DataSource='<%# CalendarIndex.DefaultView() %>' 
>           DataTextField="Intel_WW" 
>           SelectedIndex='<%# 
>GetWWIndex(Container.DataItem("pop_plan")) %>'
>/>
>         </td></tr>
>         </table>
>       </EditItemTemplate>
>     </asp:TemplateColumn>
>
>   </Columns>
></asp:DataGrid>
>
>

---------------------------------------------
http://mail.indiainfo.com
India's first ISO certified portal
Check world time at http://time.indiainfo.com

  Return to Index