only for expert: what's wrong with this code?

Discussion in 'ASP .Net Web Controls' started by Cas, Oct 25, 2006.

  1. Cas

    Cas Guest

    Hi,

    I want to use a detailsview only for inserting data into a database (for a
    survey). In order to check the inputted data, i need Templatefield.
    So I defined a detailsview and a SqlDataSource in the aspx file.
    The creation of the templatefields are done programmatically, because the
    number of fields vary (fieldnames are fetched from the same sqldatasource).

    Now, my problem: no error, the detailsview renders the right fieldheaders
    and the texboxs, but when clicking on the Inset button, i can see there is a
    postback, but the ItemInserting procedure is not started (so no inserting)
    and the texboxs are gone!!

    Thanks for help
    Cas



    The code:
    1) the class:
    -----------
    Public Class DetailsViewTemplate
    Inherits System.Web.UI.Page
    Implements ITemplate
    Dim templatetype As ListItemType
    Dim columnname As String

    Public Sub New(ByVal type As ListItemType, ByVal vg As String)
    templatetype = type
    columnname = vg
    End Sub

    Private Sub InstantiateIn(ByVal container As Control) Implements
    ITemplate.InstantiateIn
    Case ListItemType.EditItem
    Dim tb = New TextBox()
    tb.Text = ""
    container.Controls.Add(tb)
    End Select
    End Sub
    End Class

    2) code-behind
    ---------------
    Imports System.Data.OleDb
    Partial Class excel
    Inherits System.Web.UI.Page
    Friend nfield As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim sql As String
    Dim oConnection As OleDbConnection
    'Dim sConnectionString As String
    oConnection = New OleDbConnection()

    Dim comd As OleDbCommand
    Dim dtreader As OleDbDataReader

    SqlDataSource1.DataBind()
    oConnection.ConnectionString = SqlDataSource1.ConnectionString
    oConnection.Open()
    Dim i As Integer
    sql = "select count(*) from fld;"
    comd = New OleDbCommand(sql, oConnection)
    nfield = comd.ExecuteScalar
    nfieldout = nfield

    sql = "select * from fld;"
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    Dim bf(nfield) As TemplateField

    If Not Page.IsPostBack Then
    For i = 0 To nfield - 1
    dtreader.Read()
    bf(i) = New TemplateField
    bf(i).ItemTemplate = New
    DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
    bf(i).InsertItemTemplate = New
    DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
    DetailsView1.Fields.Add(bf(i))
    Next
    dtreader.Close()

    Dim cf As CommandField
    cf = New CommandField
    cf.ShowInsertButton = True
    DetailsView1.Fields.Add(cf)
    End If
    oConnection.Close()
    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    DetailsView1.ItemInserting
    Dim fd, vl, wd,inscomm, a, vlfin As String
    Dim i, j, tel As Integer
    For i = 1 To nfield
    fd= fd & "field" & i & ","
    vl = e.Values("fld" & i)
    wd = wd & "'" & vlfin & "',"
    vlfin = ""
    Next
    vragen = vragen & ") values ('" & lol & "',"
    inscomm = "insert into data (login," & fd &wd
    SqlDataSource1.InsertCommand = inscomm
    SqlDataSource1.ProviderName = "System.Data.OleDb"
    End Sub
    End Class
    Cas, Oct 25, 2006
    #1
    1. Advertising

  2. Cas

    Bob Lehmann Guest

    Re: only for expert:

    Boy, I hope an expert shows up soon.

    Bob Lehmann

    "Cas" <> wrote in message
    news:u5BHMnG%...
    > Hi,
    >
    > I want to use a detailsview only for inserting data into a database (for a
    > survey). In order to check the inputted data, i need Templatefield.
    > So I defined a detailsview and a SqlDataSource in the aspx file.
    > The creation of the templatefields are done programmatically, because the
    > number of fields vary (fieldnames are fetched from the same

    sqldatasource).
    >
    > Now, my problem: no error, the detailsview renders the right fieldheaders
    > and the texboxs, but when clicking on the Inset button, i can see there is

    a
    > postback, but the ItemInserting procedure is not started (so no inserting)
    > and the texboxs are gone!!
    >
    > Thanks for help
    > Cas
    >
    >
    >
    > The code:
    > 1) the class:
    > -----------
    > Public Class DetailsViewTemplate
    > Inherits System.Web.UI.Page
    > Implements ITemplate
    > Dim templatetype As ListItemType
    > Dim columnname As String
    >
    > Public Sub New(ByVal type As ListItemType, ByVal vg As String)
    > templatetype = type
    > columnname = vg
    > End Sub
    >
    > Private Sub InstantiateIn(ByVal container As Control) Implements
    > ITemplate.InstantiateIn
    > Case ListItemType.EditItem
    > Dim tb = New TextBox()
    > tb.Text = ""
    > container.Controls.Add(tb)
    > End Select
    > End Sub
    > End Class
    >
    > 2) code-behind
    > ---------------
    > Imports System.Data.OleDb
    > Partial Class excel
    > Inherits System.Web.UI.Page
    > Friend nfield As Integer
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim sql As String
    > Dim oConnection As OleDbConnection
    > 'Dim sConnectionString As String
    > oConnection = New OleDbConnection()
    >
    > Dim comd As OleDbCommand
    > Dim dtreader As OleDbDataReader
    >
    > SqlDataSource1.DataBind()
    > oConnection.ConnectionString = SqlDataSource1.ConnectionString
    > oConnection.Open()
    > Dim i As Integer
    > sql = "select count(*) from fld;"
    > comd = New OleDbCommand(sql, oConnection)
    > nfield = comd.ExecuteScalar
    > nfieldout = nfield
    >
    > sql = "select * from fld;"
    > comd = New OleDbCommand(sql, oConnection)
    > dtreader = comd.ExecuteReader
    > Dim bf(nfield) As TemplateField
    >
    > If Not Page.IsPostBack Then
    > For i = 0 To nfield - 1
    > dtreader.Read()
    > bf(i) = New TemplateField
    > bf(i).ItemTemplate = New
    > DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
    > bf(i).InsertItemTemplate = New
    > DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
    > DetailsView1.Fields.Add(bf(i))
    > Next
    > dtreader.Close()
    >
    > Dim cf As CommandField
    > cf = New CommandField
    > cf.ShowInsertButton = True
    > DetailsView1.Fields.Add(cf)
    > End If
    > oConnection.Close()
    > End Sub
    >
    > Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    > ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    > DetailsView1.ItemInserting
    > Dim fd, vl, wd,inscomm, a, vlfin As String
    > Dim i, j, tel As Integer
    > For i = 1 To nfield
    > fd= fd & "field" & i & ","
    > vl = e.Values("fld" & i)
    > wd = wd & "'" & vlfin & "',"
    > vlfin = ""
    > Next
    > vragen = vragen & ") values ('" & lol & "',"
    > inscomm = "insert into data (login," & fd &wd
    > SqlDataSource1.InsertCommand = inscomm
    > SqlDataSource1.ProviderName = "System.Data.OleDb"
    > End Sub
    > End Class
    >
    >
    >
    >
    Bob Lehmann, Oct 26, 2006
    #2
    1. Advertising

  3. <sarcasticmode >
    Don`t know if my expertise is considered "good enough" so i dear not to
    answer this question :)
    </sarcasticmode>

    regards
    Michel Posseth [MCP]

    "Bob Lehmann" <> schreef in bericht
    news:eL$M3kI%...
    > Re: only for expert:
    >
    > Boy, I hope an expert shows up soon.
    >
    > Bob Lehmann
    >
    > "Cas" <> wrote in message
    > news:u5BHMnG%...
    >> Hi,
    >>
    >> I want to use a detailsview only for inserting data into a database (for
    >> a
    >> survey). In order to check the inputted data, i need Templatefield.
    >> So I defined a detailsview and a SqlDataSource in the aspx file.
    >> The creation of the templatefields are done programmatically, because the
    >> number of fields vary (fieldnames are fetched from the same

    > sqldatasource).
    >>
    >> Now, my problem: no error, the detailsview renders the right fieldheaders
    >> and the texboxs, but when clicking on the Inset button, i can see there
    >> is

    > a
    >> postback, but the ItemInserting procedure is not started (so no
    >> inserting)
    >> and the texboxs are gone!!
    >>
    >> Thanks for help
    >> Cas
    >>
    >>
    >>
    >> The code:
    >> 1) the class:
    >> -----------
    >> Public Class DetailsViewTemplate
    >> Inherits System.Web.UI.Page
    >> Implements ITemplate
    >> Dim templatetype As ListItemType
    >> Dim columnname As String
    >>
    >> Public Sub New(ByVal type As ListItemType, ByVal vg As String)
    >> templatetype = type
    >> columnname = vg
    >> End Sub
    >>
    >> Private Sub InstantiateIn(ByVal container As Control) Implements
    >> ITemplate.InstantiateIn
    >> Case ListItemType.EditItem
    >> Dim tb = New TextBox()
    >> tb.Text = ""
    >> container.Controls.Add(tb)
    >> End Select
    >> End Sub
    >> End Class
    >>
    >> 2) code-behind
    >> ---------------
    >> Imports System.Data.OleDb
    >> Partial Class excel
    >> Inherits System.Web.UI.Page
    >> Friend nfield As Integer
    >>
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim sql As String
    >> Dim oConnection As OleDbConnection
    >> 'Dim sConnectionString As String
    >> oConnection = New OleDbConnection()
    >>
    >> Dim comd As OleDbCommand
    >> Dim dtreader As OleDbDataReader
    >>
    >> SqlDataSource1.DataBind()
    >> oConnection.ConnectionString = SqlDataSource1.ConnectionString
    >> oConnection.Open()
    >> Dim i As Integer
    >> sql = "select count(*) from fld;"
    >> comd = New OleDbCommand(sql, oConnection)
    >> nfield = comd.ExecuteScalar
    >> nfieldout = nfield
    >>
    >> sql = "select * from fld;"
    >> comd = New OleDbCommand(sql, oConnection)
    >> dtreader = comd.ExecuteReader
    >> Dim bf(nfield) As TemplateField
    >>
    >> If Not Page.IsPostBack Then
    >> For i = 0 To nfield - 1
    >> dtreader.Read()
    >> bf(i) = New TemplateField
    >> bf(i).ItemTemplate = New
    >> DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
    >> bf(i).InsertItemTemplate = New
    >> DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
    >> DetailsView1.Fields.Add(bf(i))
    >> Next
    >> dtreader.Close()
    >>
    >> Dim cf As CommandField
    >> cf = New CommandField
    >> cf.ShowInsertButton = True
    >> DetailsView1.Fields.Add(cf)
    >> End If
    >> oConnection.Close()
    >> End Sub
    >>
    >> Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    >> ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    >> DetailsView1.ItemInserting
    >> Dim fd, vl, wd,inscomm, a, vlfin As String
    >> Dim i, j, tel As Integer
    >> For i = 1 To nfield
    >> fd= fd & "field" & i & ","
    >> vl = e.Values("fld" & i)
    >> wd = wd & "'" & vlfin & "',"
    >> vlfin = ""
    >> Next
    >> vragen = vragen & ") values ('" & lol & "',"
    >> inscomm = "insert into data (login," & fd &wd
    >> SqlDataSource1.InsertCommand = inscomm
    >> SqlDataSource1.ProviderName = "System.Data.OleDb"
    >> End Sub
    >> End Class
    >>
    >>
    >>
    >>

    >
    >
    Michel Posseth [MCP], Oct 26, 2006
    #3
  4. Cas

    Guest

    Hi, try to do your TemplateField creation programmatically in the
    DetailView's OnInit, or Page's OnInit.... Page_Load() is not a good
    place for the creation, because dynamic controls need to be bound on
    every postback in Page_Load()

    Hope this helps...



    Regards,
    Alvin Chooi
    Microsoft ASP.NET Enthusiast
    http://alvinzc.blogspot.com
    , Oct 26, 2006
    #4
  5. Cas

    Cas Guest

    I'm sure it is. So go ahead ...
    Thanks in advance.

    "Michel Posseth [MCP]" <> schreef in bericht
    news:OR9mV%23I%...
    > <sarcasticmode >
    > Don`t know if my expertise is considered "good enough" so i dear not to
    > answer this question :)
    > </sarcasticmode>
    >
    > regards
    > Michel Posseth [MCP]
    >
    > "Bob Lehmann" <> schreef in bericht
    > news:eL$M3kI%...
    >> Re: only for expert:
    >>
    >> Boy, I hope an expert shows up soon.
    >>
    >> Bob Lehmann
    >>
    >> "Cas" <> wrote in message
    >> news:u5BHMnG%...
    >>> Hi,
    >>>
    >>> I want to use a detailsview only for inserting data into a database (for
    >>> a
    >>> survey). In order to check the inputted data, i need Templatefield.
    >>> So I defined a detailsview and a SqlDataSource in the aspx file.
    >>> The creation of the templatefields are done programmatically, because
    >>> the
    >>> number of fields vary (fieldnames are fetched from the same

    >> sqldatasource).
    >>>
    >>> Now, my problem: no error, the detailsview renders the right
    >>> fieldheaders
    >>> and the texboxs, but when clicking on the Inset button, i can see there
    >>> is

    >> a
    >>> postback, but the ItemInserting procedure is not started (so no
    >>> inserting)
    >>> and the texboxs are gone!!
    >>>
    >>> Thanks for help
    >>> Cas
    >>>
    >>>
    >>>
    >>> The code:
    >>> 1) the class:
    >>> -----------
    >>> Public Class DetailsViewTemplate
    >>> Inherits System.Web.UI.Page
    >>> Implements ITemplate
    >>> Dim templatetype As ListItemType
    >>> Dim columnname As String
    >>>
    >>> Public Sub New(ByVal type As ListItemType, ByVal vg As String)
    >>> templatetype = type
    >>> columnname = vg
    >>> End Sub
    >>>
    >>> Private Sub InstantiateIn(ByVal container As Control) Implements
    >>> ITemplate.InstantiateIn
    >>> Case ListItemType.EditItem
    >>> Dim tb = New TextBox()
    >>> tb.Text = ""
    >>> container.Controls.Add(tb)
    >>> End Select
    >>> End Sub
    >>> End Class
    >>>
    >>> 2) code-behind
    >>> ---------------
    >>> Imports System.Data.OleDb
    >>> Partial Class excel
    >>> Inherits System.Web.UI.Page
    >>> Friend nfield As Integer
    >>>
    >>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >>> System.EventArgs) Handles Me.Load
    >>> Dim sql As String
    >>> Dim oConnection As OleDbConnection
    >>> 'Dim sConnectionString As String
    >>> oConnection = New OleDbConnection()
    >>>
    >>> Dim comd As OleDbCommand
    >>> Dim dtreader As OleDbDataReader
    >>>
    >>> SqlDataSource1.DataBind()
    >>> oConnection.ConnectionString = SqlDataSource1.ConnectionString
    >>> oConnection.Open()
    >>> Dim i As Integer
    >>> sql = "select count(*) from fld;"
    >>> comd = New OleDbCommand(sql, oConnection)
    >>> nfield = comd.ExecuteScalar
    >>> nfieldout = nfield
    >>>
    >>> sql = "select * from fld;"
    >>> comd = New OleDbCommand(sql, oConnection)
    >>> dtreader = comd.ExecuteReader
    >>> Dim bf(nfield) As TemplateField
    >>>
    >>> If Not Page.IsPostBack Then
    >>> For i = 0 To nfield - 1
    >>> dtreader.Read()
    >>> bf(i) = New TemplateField
    >>> bf(i).ItemTemplate = New
    >>> DetailsViewTemplate(ListItemType.Item, "fld" & nfield)
    >>> bf(i).InsertItemTemplate = New
    >>> DetailsViewTemplate(ListItemType.EditItem, "fld" & nfield)
    >>> DetailsView1.Fields.Add(bf(i))
    >>> Next
    >>> dtreader.Close()
    >>>
    >>> Dim cf As CommandField
    >>> cf = New CommandField
    >>> cf.ShowInsertButton = True
    >>> DetailsView1.Fields.Add(cf)
    >>> End If
    >>> oConnection.Close()
    >>> End Sub
    >>>
    >>> Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    >>> ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    >>> DetailsView1.ItemInserting
    >>> Dim fd, vl, wd,inscomm, a, vlfin As String
    >>> Dim i, j, tel As Integer
    >>> For i = 1 To nfield
    >>> fd= fd & "field" & i & ","
    >>> vl = e.Values("fld" & i)
    >>> wd = wd & "'" & vlfin & "',"
    >>> vlfin = ""
    >>> Next
    >>> vragen = vragen & ") values ('" & lol & "',"
    >>> inscomm = "insert into data (login," & fd &wd
    >>> SqlDataSource1.InsertCommand = inscomm
    >>> SqlDataSource1.ProviderName = "System.Data.OleDb"
    >>> End Sub
    >>> End Class
    >>>
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
    Cas, Oct 26, 2006
    #5
  6. Cas

    Cas Guest

    Hi Alvin,

    Thanks, it helps, but now i have another problem: after the InsertCommand is
    executed (data is put into the excel database), i can't open that excel
    file. The only way is to stop IIS and restart it. I think there is still a
    open connection, but where? I thought the SqlDataSouce would be closed
    automatically after executing the InsertCommand? Anyway, i can't close it...
    Any idea?
    Thanks again.

    I give you part of code:

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Init
    oConnection = New OleDbConnection()
    SqlDataSource1.DataBind()
    oConnection.ConnectionString = SqlDataSource1.ConnectionString
    oConnection.Open()
    sql = "select count(*) from vragen;"
    comd = New OleDbCommand(sql, oConnection)
    nfield = comd.ExecuteScalar

    sql = "select * from vragen;"
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    Dim bf(nfield) As BoundField

    For i = 0 To nfield - 1
    dtreader.Read()
    bf(i) = New TemplateField
    .....
    DetailsView1.Fields.Add(bf(i))
    Next
    dtreader.Close()
    oConnection.Close()
    'closed !
    End Sub
    '---------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    oConnection = New OleDbConnection()
    SqlDataSource1.DataBind()
    oConnection.ConnectionString = SqlDataSource1.ConnectionString
    oConnection.Open()
    sql = "select login from data;"
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    .......
    oConnection.Close()
    'closed !
    End Sub
    '----------------------------------
    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object,
    ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
    DetailsView1.ItemInserting
    Dim inscomm As String
    inscomm = "insert into data (field1, field2) values ('ok','ok2')"
    SqlDataSource1.InsertCommand = inscomm
    SqlDataSource1.ProviderName = "System.Data.OleDb"
    'how to close here?
    End Sub
    End Class
    Cas, Oct 27, 2006
    #6
  7. Cas

    Guest

    Hi,

    Why dont you put the ItemCommand declaractively , rather
    programmatically in the ItemInserting event? Could you post your
    SqlDataSource declaration in aspx here?


    Hope this helps...


    Regards,
    Alvin Chooi
    Microsoft ASP.NET Enthusiast
    http://alvinzc.blogspot.com
    , Oct 27, 2006
    #7
  8. Cas

    Cas Guest

    I do this programmatically, because i never know how many fields there will
    be (the questions of the survey are are fetched from the database), so i
    build the InsertCommand with strings with all the fields.

    The aspx code is qiute usual:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="Provider = Microsoft.Jet.OLEDB.4.0; Data Source =
    c:\ws1PR.xls; Extended Properties=Excel 8.0;">
    </asp:SqlDataSource>

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
    CellPadding="3"
    DataSourceID="SqlDataSource1" DefaultMode="Insert">
    </asp:DetailsView>


    <> schreef in bericht
    news:...
    > Hi,
    >
    > Why dont you put the ItemCommand declaractively , rather
    > programmatically in the ItemInserting event? Could you post your
    > SqlDataSource declaration in aspx here?
    >
    >
    > Hope this helps...
    >
    >
    > Regards,
    > Alvin Chooi
    > Microsoft ASP.NET Enthusiast
    > http://alvinzc.blogspot.com
    >
    Cas, Oct 27, 2006
    #8
    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. joker
    Replies:
    3
    Views:
    379
  2. Cas

    only for expert: what's wrong with this code?

    Cas, Oct 25, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    727
  3. Tony Gilbert

    What am I doing wrong - expert question

    Tony Gilbert, Nov 21, 2003, in forum: Javascript
    Replies:
    0
    Views:
    104
    Tony Gilbert
    Nov 21, 2003
  4. QA
    Replies:
    4
    Views:
    114
    Randy Webb
    Sep 11, 2004
  5. Great Deals

    Expert: What is wrong here?

    Great Deals, Sep 24, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    117
    Jay Tilton
    Sep 25, 2003
Loading...

Share This Page