Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 29th, 2003, 07:16 AM
Registered User
 
Join Date: Aug 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Datalist and web form

:)HI,
i have one problem with datalist; I using vb.net and odbc.net to connetting to progress database and pubblishing data to aspx page in a datalist object . And this is ok, I can looking data, but if I try to use the data on datalist to set a simple textbox , the textbox
is set on first datalist data or blank data ? why ?

Help me to set textbox to select datalist data .

Grazie . Ciao .
:)

:)
 
Old August 30th, 2003, 12:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I guess u dont set Selected Item. Hope this sample can help u although I didnt get u clearly.

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

<Script Runat="Server">
  Sub Page_Load( s As Object, e As EventArgs )
    If Not isPostBack Then
      BindData
    End If
  End Sub

  Sub BindData
    Dim myConnection As SqlConnection
    Dim myCommand As SQLCommand
    myConnection = New SqlConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
    myCommand = New SqlCommand( "Select au_id, au_lname, au_fname, phone from Authors order by au_lname", myConnection )
    myConnection.Open()
    myDataList.DataSource = myCommand.ExecuteReader()
    myDataList.DataBind()
    myConnection.Close()
  End Sub

  Sub editAuthor( s As Object, e As DataListCommandEventArgs )
    myDataList.EditItemIndex = e.Item.ItemIndex
    BindData
  End Sub

  Sub cancelEdit( s As Object, e As DataListCommandEventArgs )
    myDataList.EditItemIndex = -1
    BindData
  End Sub

  Sub deleteAuthor( s As Object, e As DataListCommandEventArgs )
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim sqlString As String
    myConnection = New SqlConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
    sqlString = "Delete Authors Where au_id=@authorID"
    myCommand = New SqlCommand( sqlString, myConnection )
    myCommand.Parameters.Add( New SQLParameter( "@authorID", SqlDbType.VarChar, 11 ))
    myCommand.Parameters( "@authorID" ).Value = myDataList.DataKeys.Item( e.Item.ItemIndex )
    myConnection.Open()
    myCommand.ExecuteNonQuery
    myDataList.DataBind()
    myConnection.Close()
    myDataList.EditItemIndex = -1
    BindData
  End Sub

  Sub updateAuthor( s As Object, e As DataListCommandEventArgs )
    Dim myConnection As SQLConnection
    Dim myCommand As SQLCommand
    Dim sqlString As String
    myConnection = New SQLConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
    sqlString = "Update Authors Set au_lname=@lastname, au_fname=@firstname, phone=@phone" _
      & " Where au_id=@authorID"
    myCommand = New SQLCommand( sqlString, myConnection )
    myCommand.Parameters.Add( New SQLParameter( "@lastname", SqlDbType.VarChar, 40 ))
    myCommand.Parameters( "@lastname" ).Value = cTYPE( e.Item.FindControl( "lastname" ), textBox ).Text
    myCommand.Parameters.Add( New SQLParameter( "@firstname", SqlDbType.VarChar, 20 ))
    myCommand.Parameters( "@firstname" ).Value = cTYPE( e.Item.FindControl( "firstname" ), textBox ).Text
    myCommand.Parameters.Add( New SQLParameter( "@phone", SqlDbType.Char, 12 ))
    myCommand.Parameters( "@phone" ).Value = cTYPE( e.Item.FindControl( "phone" ), textBox ).Text
    myCommand.Parameters.Add( New SQLParameter( "@authorID", SqlDbType.VarChar, 11 ))
    myCommand.Parameters( "@authorID" ).Value = myDataList.DataKeys.Item( e.Item.ItemIndex )
    myConnection.Open()
    myCommand.ExecuteNonQuery
    myDataList.DataBind()
    myConnection.Close()
    myDataList.EditItemIndex = -1
    BindData
  End Sub
</Script>

<html>
<head><title>Edit Authors</title></head>
<body>

<form Runat="Server">

<asp:DataList
  id="myDataList"
  cellpadding=10
  cellspacing=0
  gridlines="both"
  RepeatColumns="3"
  RepeatDirection="Horizontal"
  DataKeyField="au_id"
  OnEditCommand="editAuthor"
  OnDeleteCommand="deleteAuthor"
  OnUpdateCommand="updateAuthor"
  OnCancelCommand="cancelEdit"
  Runat="Server">

  <ItemTemplate>
    <asp:LinkButton
      Text="Edit"
      CommandName="edit"
      Runat="Server"/>
    <%# Container.DataItem( "au_lname" )%>
  </ItemTemplate>

  <EditItemTemplate>
     <b>Last Name:</b>
     <br><asp:TextBox
       id="lastname"
       text='<%# Container.DataItem( "au_lname" ) %>'
       Runat="Server"/>
     <p>
     <b>First Name:</b>
     <br><asp:TextBox
       id="firstname"
       text='<%# Container.DataItem( "au_fname" ) %>'
       Runat="Server"/>
     <p>
     <b>Phone:</b>
     <br><asp:TextBox
       id="phone"
       text='<%# Container.DataItem( "phone" ) %>'
       Runat="Server"/>
     <p>
     <asp:Button
       Text="Update"
       CommandName="update"
       Runat="Server"/>
     <asp:Button
       Text="Delete"
       CommandName="delete"
       Runat="Server"/>
     <asp:Button
       Text="Cancel"
       CommandName="cancel"
       Runat="Server"/>
  </EditItemTemplate>

</asp:DataList>

</form>

</body>
</html>
Always:),
Hovik Melkomian.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to insert web user control in DataList mehrdad_T ASP.NET 2.0 Basics 2 June 25th, 2007 07:20 AM
DataList Web control causes screen flicker Jan_Ma Classic ASP Basics 1 July 15th, 2003 03:47 PM
DataList Web Control causes screen flicker Jan_Ma BOOK: ASP.NET Website Programming Problem-Design-Solution 0 June 30th, 2003 04:39 PM
DataList Web control causes screen flicker Jan_Ma Classic ASP Professional 0 June 30th, 2003 04:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.