Home All Groups Group Topic Archive Search About
Author
16 Oct 2006 9:15 AM
bob
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

Author
23 Oct 2006 8:17 PM
Phil H
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