what' wrong with this?

Discussion in 'ASP .Net Web Controls' started by bob, Oct 16, 2006.

  1. bob

    bob Guest

    Hi,

    I try ro insert programmatically data into 2 fields of a Access table from
    a detailsview.
    My problem is: when clicking on the 'insert' button, nothing happens (no
    error but no data inserted).
    When 'burning' the values in the sql command, it works, but when using the
    inputted values in the detailsview, it doesn't.
    The problem is inthe line:: field1 = e.Values("field1") which renders
    nothing, i think.

    Thanks for helping
    Bob

    Here the code:
    --------------
    In the aspx file:

    <form id="form1" runat="server">
    <asp:DetailsView ID="DetailsView1" runat="server" defaultmode="Insert"
    AutoGenerateInsertButton="True">
    </asp:DetailsView>
    </form>


    In the aspx.vb file::

    Imports System.Data.OleDb
    Partial Class test
    Inherits System.Web.UI.Page
    Friend oConnection As OleDbConnection
    Friend sConnectionString As String
    Friend sql As String
    Friend comd As OleDbCommand
    Friend dtreader As OleDbDataReader
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    oConnection = New OleDbConnection()
    sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =
    c:\mydb.mdb; Jet OLEDB;"
    sql = "select field1,field2 from mytable;"
    oConnection.ConnectionString = sConnectionString
    oConnection.Open()
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    DetailsView1.DataSource = dtreader
    DetailsView1.DataBind()
    oConnection.Close()
    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    DetailsView1.ItemInserting
    oConnection.Open()
    Dim field1, field2 As String
    field1 = e.Values("field1")
    field2 = e.Values("field2")
    'sql = "insert into pc (field1,field2) values('ok','ok2')"
    'this works
    sql = "insert into pc (field1,field2) values('" & naam & "','" &
    lokaal & "')"
    comd = New OleDbCommand(sql, oConnection)
    comd.ExecuteNonQuery()
    dtreader.Close()
    oConnection.Close()
    End Sub
     
    bob, Oct 16, 2006
    #1
    1. Advertising

  2. bob

    Phil H Guest

    Hi Bob

    A DetailsView object is meant to be attached to a DataSource. In your
    case the most suitable type would be an AccessDataSource.

    The AccessDataSource will have an InsertQuery property which you need
    to configure. It is a string that comprises an SQL INSERT command
    (usually) with parameters. The parameters can be set so that they are
    read from the text boxes of your details view.

    If you are using VS2005 then all this is made very easy using various
    wizard and dialogues.

    The example code you give doesn't contain any settings to indicate
    where the DetailsView object gets its data from nor where it sends it.

    Is that any help?

    Phil Hall
     
    Phil H, Oct 23, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salisha Khan
    Replies:
    1
    Views:
    11,672
    JohnFol
    Aug 1, 2003
  2. Replies:
    2
    Views:
    397
  3. Pial
    Replies:
    4
    Views:
    56,865
    Phatman
    Aug 27, 2010
  4. Paul Leventis

    Re: Quartus bug or wrong VHDL?

    Paul Leventis, Jun 24, 2003, in forum: VHDL
    Replies:
    0
    Views:
    2,492
    Paul Leventis
    Jun 24, 2003
  5. Matthew
    Replies:
    7
    Views:
    742
    Priscilla Walmsley
    Jan 7, 2005
Loading...

Share This Page