Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > only for expert: what's wrong with this code?

Reply
Thread Tools

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

 
 
Cas
Guest
Posts: n/a
 
      10-25-2006
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.DetailsViewInsertEventAr gs) 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




 
Reply With Quote
 
 
 
 
Bob Lehmann
Guest
Posts: n/a
 
      10-25-2006
Re: only for expert:

Boy, I hope an expert shows up soon.

Bob Lehmann

"Cas" <(E-Mail Removed)> wrote in message
news:u5BHMnG%(E-Mail Removed)...
> 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.DetailsViewInsertEventAr gs) 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
>
>
>
>



 
Reply With Quote
 
 
 
 
Michel Posseth [MCP]
Guest
Posts: n/a
 
      10-25-2006
<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" <(E-Mail Removed)> schreef in bericht
news:eL$M3kI%(E-Mail Removed)...
> Re: only for expert:
>
> Boy, I hope an expert shows up soon.
>
> Bob Lehmann
>
> "Cas" <(E-Mail Removed)> wrote in message
> news:u5BHMnG%(E-Mail Removed)...
>> 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.DetailsViewInsertEventAr gs) 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
>>
>>
>>
>>

>
>



 
Reply With Quote
 
alvinzc@gmail.com
Guest
Posts: n/a
 
      10-26-2006
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

 
Reply With Quote
 
Cas
Guest
Posts: n/a
 
      10-26-2006
I'm sure it is. So go ahead ...
Thanks in advance.

"Michel Posseth [MCP]" <(E-Mail Removed)> schreef in bericht
news:OR9mV%23I%(E-Mail Removed)...
> <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" <(E-Mail Removed)> schreef in bericht
> news:eL$M3kI%(E-Mail Removed)...
>> Re: only for expert:
>>
>> Boy, I hope an expert shows up soon.
>>
>> Bob Lehmann
>>
>> "Cas" <(E-Mail Removed)> wrote in message
>> news:u5BHMnG%(E-Mail Removed)...
>>> 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.DetailsViewInsertEventAr gs) 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
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Cas
Guest
Posts: n/a
 
      10-27-2006
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.DetailsViewInsertEventAr gs) 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



 
Reply With Quote
 
alvinzc@gmail.com
Guest
Posts: n/a
 
      10-27-2006
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

 
Reply With Quote
 
Cas
Guest
Posts: n/a
 
      10-27-2006
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>

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


<(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) oups.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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python-parser running Beautiful Soup only spits out one line of 10.What i have gotten wrong here? Martin Kaspar Python 1 12-25-2010 06:36 PM
only for expert: what's wrong with this code? Cas ASP .Net Datagrid Control 3 10-26-2006 03:57 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
Is JAVAscript the only thing that could wrong under a html page? Norberto Rivera Javascript 5 08-20-2004 01:17 PM
newbie question-----where is wrong.(only 19 lines code) Facco Eloelo Perl Misc 3 08-13-2004 08:50 PM



Advertisments