Home All Groups Group Topic Archive Search About

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

Author
25 Oct 2006 7:14 PM
Cas
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

Author
25 Oct 2006 11:00 PM
Bob Lehmann
Re: only for expert:

Boy, I hope an expert shows up soon.

Bob Lehmann

Show quoteHide quote
"Cas" <c**@qscq.op> wrote in message
news:u5BHMnG%23GHA.2300@TK2MSFTNGP04.phx.gbl...
> 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
>
>
>
>
Author
25 Oct 2006 11:45 PM
Michel Posseth [MCP]
<sarcasticmode >
Don`t know if my expertise is considered "good enough" so i dear not to
answer this question :-)
</sarcasticmode>

regards
Michel Posseth [MCP]

Show quoteHide quote
"Bob Lehmann" <nospam@dontbotherme.zzz> schreef in bericht
news:eL$M3kI%23GHA.4464@TK2MSFTNGP02.phx.gbl...
> Re: only for expert:
>
> Boy, I hope an expert shows up soon.
>
> Bob Lehmann
>
> "Cas" <c**@qscq.op> wrote in message
> news:u5BHMnG%23GHA.2300@TK2MSFTNGP04.phx.gbl...
>> 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
>>
>>
>>
>>
>
>
Author
26 Oct 2006 3:57 PM
Cas
I'm sure it is. So go ahead ...
Thanks in advance.

Show quoteHide quote
"Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
news:OR9mV%23I%23GHA.4712@TK2MSFTNGP03.phx.gbl...
> <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" <nospam@dontbotherme.zzz> schreef in bericht
> news:eL$M3kI%23GHA.4464@TK2MSFTNGP02.phx.gbl...
>> Re: only for expert:
>>
>> Boy, I hope an expert shows up soon.
>>
>> Bob Lehmann
>>
>> "Cas" <c**@qscq.op> wrote in message
>> news:u5BHMnG%23GHA.2300@TK2MSFTNGP04.phx.gbl...
>>> 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
>>>
>>>
>>>
>>>
>>
>>
>
>
Author
26 Oct 2006 2:43 AM
alvinzc
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
Author
27 Oct 2006 7:10 AM
Cas
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
Author
27 Oct 2006 10:02 AM
alvinzc
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
Author
27 Oct 2006 2:31 PM
Cas
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>


<alvi***@gmail.com> schreef in bericht
Show quoteHide quote
news:1161943326.943880.142140@h48g2000cwc.googlegroups.com...
> 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
>