all groups > asp.net webcontrols > october 2006 >
You're in the

asp.net webcontrols

group:

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



Re: only for expert: what's wrong with this code? Bob Lehmann
10/25/2006 5:00:25 PM
asp.net webcontrols: Re: only for expert:

Boy, I hope an expert shows up soon.

Bob Lehmann

[quoted text, click to view]

Re: only for expert: what's wrong with this code? alvinzc NO[at]SPAM gmail.com
10/25/2006 7:43:03 PM
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
only for expert: what's wrong with this code? Cas
10/25/2006 9:14:26 PM
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



Re: only for expert: what's wrong with this code? Michel Posseth [MCP]
10/26/2006 1:45:11 AM
<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...
[quoted text, click to view]

Re: only for expert: what's wrong with this code? Cas
10/27/2006 9:10:50 AM
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


AddThis Social Bookmark Button