Showing posts with label head. Show all posts
Showing posts with label head. Show all posts

Saturday, March 24, 2012

trouble with click events on dynamically created link buttons

I've read quite a few different message on various boards and for some
reason I'm still having trouble wrapping my head around this viewstate
maintenance and trying to get these dynamically created link buttons
to stay wired up to their click events.

I have what is basically a simply survey question generation page. The
page first displays a few static fields and a dropdownlist of various
options for the user to select. When the user selects an option from
the list the page will generate a new table with 5 rows of textboxes,
drop down lists, and link buttons (to delete a row if desired). There
is also a static insert button to allow users to add additional rows
if needed.

Saving the data in the fields during postback isn't an issue, but I'm
stuck in two situations depending on how I adjust the code. First is
that I put the rebuilding of the controls in the Page_load and users
are forced to click twice on the static Insert Row button to add a row
or they have to click twice on a dynamic Delete Row link button to
remove a row. If I take the rebuilding of the controls out of the
Page_Load then the Insert Row button works fine, but clicking on a
Delete Row link button causes the click event to not fire and all the
dynamic controls disappear from the page.

Does anyone have any suggestions on what I need to do to fix this so
it's written correctly and will operate as intended? (if you need more
detail or code please ask)

Thank you for your help.

--Code Snippets (this setup requires 2 clicks on a button before the
click event appears to do anything--

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
LoadQuestionTypes()
End If

RebuildControls()

End Sub
--------
Private Sub ddlQuestionType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlQuestionType.SelectedIndexChanged

...
BuildEmptyFive()
...

End Sub
--------
Private Sub BuildEmptyFive()

Dim IDArray As New ArrayList
Dim tblAnswers As New Table
Dim x As Integer

For x = 1 To 5

Dim row As New TableRow
Dim ID As String

ID = Left(System.Guid.NewGuid.ToString, 8)

Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" & ID, 140))

Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" & ID, 150,
"Family"))

Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" & ID, 150,
"Attributes"))

Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" & ID, 40))

Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" & ID))

row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)

tblAnswers.Rows.Add(row)
IDArray.Add(ID)
Next

plhDynControls.Controls.Add(tblAnswers)

'Insert Array containing ID of each row
If IsNothing(ViewState.Item("IDArray")) Then
ViewState.Add("IDArray", IDArray)
Else
ViewState.Item("IDArray") = IDArray
End If

End Sub
----------
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click

'Add a new ID to the viewstate which will cause a new row to be
inserted when the viewstate is rebuilt
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
IDArray.Add(Left(Guid.NewGuid.ToString, 8))
ViewState.Item("IDArray") = IDArray

'RebuildControls() 'unremark this and remove from page_load to get
insert button to work perfectly (delete no workie though)

End If

End Sub
----------
Private Sub lnkDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)

Dim ID As String = Right(CType(sender, LinkButton).ID.ToString, 8)
IDArray.RemoveAt(IDArray.IndexOf(ID))

ViewState.Item("IDArray") = IDArray

End Sub
-----------
this is how I generate the link button dynamically
Private Function BuildLinkButton(ByVal name As String) As LinkButton

Dim lnkLink As New LinkButton
lnkLink.ID = name
lnkLink.Text = "Delete"
AddHandler lnkLink.Click, AddressOf lnkDelete_Click

Return lnkLink

End Function
-----------
Private Sub RebuildControls()

If IsNothing(ViewState.Item("IDArray")) Then
Exit Sub
End If

Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)

Dim tblAnswers As New Table
Dim x As Integer

For x = 0 To IDArray.Count - 1

Dim row As New TableRow

Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" &
Convert.ToString(IDArray.Item(x)), 140, Request.Form.Item("txtChoice-"
& Convert.ToString(IDArray.Item(x)))))

Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" &
Convert.ToString(IDArray.Item(x)), 150, "Family",
Request.Form.Item("ddlFamily-" & Convert.ToString(IDArray.Item(x)))))

Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)), 150, "Attributes",
Request.Form.Item("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)))))

Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" &
Convert.ToString(IDArray.Item(x)), 40, Request.Form.Item("txtScore-" &
Convert.ToString(IDArray.Item(x)))))

Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" &
Convert.ToString(IDArray.Item(x))))

row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)

tblAnswers.Rows.Add(row)

Next

plhDynControls.Controls.Add(tblAnswers)

End Sub
---------
Let me know if seeing anything else might help. Thanks again.Amoril

You cannot use NewGuid function for ids because it'll generate different id
on every call (it means also on every postback) so events for all dynamically
created controls will not be fired. And you want be able to find a value
entered by the user. Use x (loop counter) with contact prefix instead. Have
also in mind you should recreate controls in page_init (but do not access
viewstate at this stage because it's simply not collected yet) as they will
automatically recreate their state.

Hope it helps

"Amoril" wrote:

Quote:

Originally Posted by

I've read quite a few different message on various boards and for some
reason I'm still having trouble wrapping my head around this viewstate
maintenance and trying to get these dynamically created link buttons
to stay wired up to their click events.
>
I have what is basically a simply survey question generation page. The
page first displays a few static fields and a dropdownlist of various
options for the user to select. When the user selects an option from
the list the page will generate a new table with 5 rows of textboxes,
drop down lists, and link buttons (to delete a row if desired). There
is also a static insert button to allow users to add additional rows
if needed.
>
Saving the data in the fields during postback isn't an issue, but I'm
stuck in two situations depending on how I adjust the code. First is
that I put the rebuilding of the controls in the Page_load and users
are forced to click twice on the static Insert Row button to add a row
or they have to click twice on a dynamic Delete Row link button to
remove a row. If I take the rebuilding of the controls out of the
Page_Load then the Insert Row button works fine, but clicking on a
Delete Row link button causes the click event to not fire and all the
dynamic controls disappear from the page.
>
Does anyone have any suggestions on what I need to do to fix this so
it's written correctly and will operate as intended? (if you need more
detail or code please ask)
>
Thank you for your help.
>
--Code Snippets (this setup requires 2 clicks on a button before the
click event appears to do anything--
>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
>
If Not IsPostBack Then
LoadQuestionTypes()
End If
>
RebuildControls()
>
End Sub
--------
Private Sub ddlQuestionType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlQuestionType.SelectedIndexChanged
>
...
BuildEmptyFive()
...
>
End Sub
--------
Private Sub BuildEmptyFive()
>
Dim IDArray As New ArrayList
Dim tblAnswers As New Table
Dim x As Integer
>
For x = 1 To 5
>
Dim row As New TableRow
Dim ID As String
>
ID = Left(System.Guid.NewGuid.ToString, 8)
>
Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" & ID, 140))
>
Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" & ID, 150,
"Family"))
>
Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" & ID, 150,
"Attributes"))
>
Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" & ID, 40))
>
Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" & ID))
>
row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)
>
tblAnswers.Rows.Add(row)
IDArray.Add(ID)
Next
>
plhDynControls.Controls.Add(tblAnswers)
>
'Insert Array containing ID of each row
If IsNothing(ViewState.Item("IDArray")) Then
ViewState.Add("IDArray", IDArray)
Else
ViewState.Item("IDArray") = IDArray
End If
>
End Sub
----------
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
>
'Add a new ID to the viewstate which will cause a new row to be
inserted when the viewstate is rebuilt
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
IDArray.Add(Left(Guid.NewGuid.ToString, 8))
ViewState.Item("IDArray") = IDArray
>
'RebuildControls() 'unremark this and remove from page_load to get
insert button to work perfectly (delete no workie though)
>
End If
>
End Sub
----------
Private Sub lnkDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
>
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
>
Dim ID As String = Right(CType(sender, LinkButton).ID.ToString, 8)
IDArray.RemoveAt(IDArray.IndexOf(ID))
>
ViewState.Item("IDArray") = IDArray
>
End Sub
-----------
this is how I generate the link button dynamically
Private Function BuildLinkButton(ByVal name As String) As LinkButton
>
Dim lnkLink As New LinkButton
lnkLink.ID = name
lnkLink.Text = "Delete"
AddHandler lnkLink.Click, AddressOf lnkDelete_Click
>
Return lnkLink
>
End Function
-----------
Private Sub RebuildControls()
>
If IsNothing(ViewState.Item("IDArray")) Then
Exit Sub
End If
>
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
>
Dim tblAnswers As New Table
Dim x As Integer
>
For x = 0 To IDArray.Count - 1
>
Dim row As New TableRow
>
Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" &
Convert.ToString(IDArray.Item(x)), 140, Request.Form.Item("txtChoice-"
& Convert.ToString(IDArray.Item(x)))))
>
Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" &
Convert.ToString(IDArray.Item(x)), 150, "Family",
Request.Form.Item("ddlFamily-" & Convert.ToString(IDArray.Item(x)))))
>
Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)), 150, "Attributes",
Request.Form.Item("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)))))
>
Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" &
Convert.ToString(IDArray.Item(x)), 40, Request.Form.Item("txtScore-" &
Convert.ToString(IDArray.Item(x)))))
>
Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" &
Convert.ToString(IDArray.Item(x))))
>
row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)
>
tblAnswers.Rows.Add(row)
>
Next
>
plhDynControls.Controls.Add(tblAnswers)
>
End Sub
---------
Let me know if seeing anything else might help. Thanks again.
>
>


The only place that I use NewGuid to assign the ID's is in the
BuildEmptyFive sub (only fired after the user selects an item from the
drop down), for RebuildingControls sub I pull the ID's out of the
IDArray in the ViewState, so that shouldn't be an issue.

Moving the RebuildControls() sub from Page_Load to Page_Init actually
made the issue worse, now when I click on the static Insert Row button
or the dynamics link buttons to delete a row, all the dynamic controls
disappear. The static button fires it's event, but the link buttons
don't. Perhaps I'm not understanding what you mean by that since
without accessing the IDArray in the viewstate I won't know how many
controls need to be recreated.

Any more detail you could provide would be appreciated.
Hi again,

Oh yes, you're right but no need for that. it's easier to use row index and
a constant prefix for a particular control type (attribute, score,etc). I'll
try to provide a fully working example later on today.

take care
--
Milosz

"Amoril" wrote:

Quote:

Originally Posted by

The only place that I use NewGuid to assign the ID's is in the
BuildEmptyFive sub (only fired after the user selects an item from the
drop down), for RebuildingControls sub I pull the ID's out of the
IDArray in the ViewState, so that shouldn't be an issue.
>
Moving the RebuildControls() sub from Page_Load to Page_Init actually
made the issue worse, now when I click on the static Insert Row button
or the dynamics link buttons to delete a row, all the dynamic controls
disappear. The static button fires it's event, but the link buttons
don't. Perhaps I'm not understanding what you mean by that since
without accessing the IDArray in the viewstate I won't know how many
controls need to be recreated.
>
Any more detail you could provide would be appreciated.
>
>


Hi again,

Actually we have to use guid because you can delete row, which i didn't pick
up before. Anyway, i created fully working example for you. You should be
fine from this point

-- begin aspx code --

<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="false" CodeFile="Survey.aspx.vb"
Inherits="Survey" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="questions" AutoPostBack="true">
<asp:ListItem Text="Please Select a Question..." />
<asp:ListItem Text="What are your names?" />
<asp:ListItem Text="Name all girlfriends you have had in your life" />
</asp:DropDownList>
<asp:Panel runat="server" ID="container" />
<asp:Panel runat="server" ID="surveyOptions">
<asp:Button ID="btnAddRow" runat="server" Text="Add row" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Survey"/>
</asp:Panel>
</div>
</form>
</body>
</html>
-- end aspx code --

-- begin vb.net code --

Partial Class Survey
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
RecreateRows()
End Sub

Protected Sub questions_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles questions.SelectedIndexChanged

Const DefaultRowCount As Integer = 5

' clear everything
IDs.Clear()

If CType(sender, DropDownList).SelectedIndex >= 0 Then
' create x default empty rows
For i As Integer = 1 To DefaultRowCount
IDs.Add(GenerateId())
Next
End If

RecreateRows()

End Sub

Private Sub RecreateRows()

container.Controls.Clear()

For Each id As String In IDs
AddAnswerRow(id)
Next

surveyOptions.Visible = IDs.Count 0

End Sub

Private Const RowIdPrefix As String = "row"
Private Const TextBoxIdPrefix As String = "txt"
Private Const DropDownListIdPrefix As String = "ddl"

Private Sub AddAnswerRow(ByVal id As String)

Dim panel As Panel
Dim textBox As TextBox
Dim linkButton As LinkButton
Dim dropDownList As DropDownList

' row panel
panel = New Panel()
panel.ID = RowIdPrefix & id

' answer text box
textBox = New TextBox()
textBox.ID = TextBoxIdPrefix & id

' delete button
linkButton = New LinkButton()
linkButton.ID = "btn" & id
linkButton.Text = "delete"
linkButton.CommandArgument = id
AddHandler linkButton.Command, New CommandEventHandler(AddressOf
DeleteAnswerRow)

dropDownList = New DropDownList()
dropDownList.ID = DropDownListIdPrefix & id
dropDownList.Items.Add(New ListItem("Value0", "0"))
dropDownList.Items.Add(New ListItem("Value1", "1"))
dropDownList.Items.Add(New ListItem("Value2", "2"))

panel.Controls.Add(textBox)
panel.Controls.Add(dropDownList)
panel.Controls.Add(linkButton)
container.Controls.Add(panel)

End Sub

Private Sub DeleteAnswerRow(ByVal source As Object, ByVal e As
CommandEventArgs)

Dim id As String = CType(e.CommandArgument, String)
Dim control As Control = container.FindControl(RowIdPrefix & id)

If (Not control Is Nothing) Then
container.Controls.Remove(control)

Dim index As Integer = IDs.IndexOf(id)
If index <-1 Then
IDs.RemoveAt(index)
End If

End If

End Sub

Private ReadOnly Property IDs() As ArrayList
Get
Dim value As Object = ViewState("IDs")
If value Is Nothing Then
value = New ArrayList()
ViewState("IDs") = value
End If
Return value
End Get
End Property

Private Function GenerateId() As String
Return Guid.NewGuid().ToString("N")
End Function

Protected Sub btnAddRow_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAddRow.Click

Dim id As String = GenerateId()

IDs.Add(id)
AddAnswerRow(id)

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click

' obtain results
Dim textBox As TextBox
Dim dropDownList As DropDownList

For Each id As String In IDs

'
' text box value
'
textBox = CType(container.FindControl(TextBoxIdPrefix & id), TextBox)

If (Not textBox Is Nothing) Then
Dim textBoxValue As String = textBox.Text
End If

'
' drop down list selected value
'
dropDownList = CType(container.FindControl(DropDownListIdPrefix & id),
DropDownList)

If (Not dropDownList Is Nothing) Then
Dim dropDownListValue As String = dropDownList.SelectedValue
End If

Next

End Sub

End Class

-- end vb.net code --
Milosz

"Milosz Skalecki [MCAD]" wrote:

Quote:

Originally Posted by

Hi again,
>
Oh yes, you're right but no need for that. it's easier to use row index and
a constant prefix for a particular control type (attribute, score,etc). I'll
try to provide a fully working example later on today.
>
take care
--
Milosz
>
>
"Amoril" wrote:
>

Quote:

Originally Posted by

The only place that I use NewGuid to assign the ID's is in the
BuildEmptyFive sub (only fired after the user selects an item from the
drop down), for RebuildingControls sub I pull the ID's out of the
IDArray in the ViewState, so that shouldn't be an issue.

Moving the RebuildControls() sub from Page_Load to Page_Init actually
made the issue worse, now when I click on the static Insert Row button
or the dynamics link buttons to delete a row, all the dynamic controls
disappear. The static button fires it's event, but the link buttons
don't. Perhaps I'm not understanding what you mean by that since
without accessing the IDArray in the viewstate I won't know how many
controls need to be recreated.

Any more detail you could provide would be appreciated.


Excellent, thank you very much for your help, it's working great.

trouble with click events on dynamically created link buttons

I've read quite a few different message on various boards and for some
reason I'm still having trouble wrapping my head around this viewstate
maintenance and trying to get these dynamically created link buttons
to stay wired up to their click events.
I have what is basically a simply survey question generation page. The
page first displays a few static fields and a dropdownlist of various
options for the user to select. When the user selects an option from
the list the page will generate a new table with 5 rows of textboxes,
drop down lists, and link buttons (to delete a row if desired). There
is also a static insert button to allow users to add additional rows
if needed.
Saving the data in the fields during postback isn't an issue, but I'm
stuck in two situations depending on how I adjust the code. First is
that I put the rebuilding of the controls in the Page_load and users
are forced to click twice on the static Insert Row button to add a row
or they have to click twice on a dynamic Delete Row link button to
remove a row. If I take the rebuilding of the controls out of the
Page_Load then the Insert Row button works fine, but clicking on a
Delete Row link button causes the click event to not fire and all the
dynamic controls disappear from the page.
Does anyone have any suggestions on what I need to do to fix this so
it's written correctly and will operate as intended? (if you need more
detail or code please ask)
Thank you for your help.
--Code Snippets (this setup requires 2 clicks on a button before the
click event appears to do anything--
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
LoadQuestionTypes()
End If
RebuildControls()
End Sub
--
Private Sub ddlQuestionType_SelectedIndexChanged(ByV
al sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlQuestionType.SelectedIndexChanged
...
BuildEmptyFive()
...
End Sub
--
Private Sub BuildEmptyFive()
Dim IDArray As New ArrayList
Dim tblAnswers As New Table
Dim x As Integer
For x = 1 To 5
Dim row As New TableRow
Dim ID As String
ID = Left(System.Guid.NewGuid.ToString, 8)
Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" & ID, 140))
Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" & ID, 150,
"Family"))
Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" & ID, 150,
"Attributes"))
Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" & ID, 40))
Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" & ID))
row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)
tblAnswers.Rows.Add(row)
IDArray.Add(ID)
Next
plhDynControls.Controls.Add(tblAnswers)
'Insert Array containing ID of each row
If IsNothing(ViewState.Item("IDArray")) Then
ViewState.Add("IDArray", IDArray)
Else
ViewState.Item("IDArray") = IDArray
End If
End Sub
--
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnInsert.Click
'Add a new ID to the viewstate which will cause a new row to be
inserted when the viewstate is rebuilt
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
IDArray.Add(Left(Guid.NewGuid.ToString, 8))
ViewState.Item("IDArray") = IDArray
'RebuildControls() 'unremark this and remove from page_load to get
insert button to work perfectly (delete no workie though)
End If
End Sub
--
Private Sub lnkDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
Dim ID As String = Right(CType(sender, LinkButton).ID.ToString, 8)
IDArray.RemoveAt(IDArray.IndexOf(ID))
ViewState.Item("IDArray") = IDArray
End Sub
--
this is how I generate the link button dynamically
Private Function BuildLinkButton(ByVal name As String) As LinkButton
Dim lnkLink As New LinkButton
lnkLink.ID = name
lnkLink.Text = "Delete"
AddHandler lnkLink.Click, AddressOf lnkDelete_Click
Return lnkLink
End Function
--
Private Sub RebuildControls()
If IsNothing(ViewState.Item("IDArray")) Then
Exit Sub
End If
Dim IDArray As ArrayList
IDArray = CType(ViewState.Item("IDArray"), ArrayList)
Dim tblAnswers As New Table
Dim x As Integer
For x = 0 To IDArray.Count - 1
Dim row As New TableRow
Dim cell1 As New TableCell
cell1.Controls.Add(BuildTextBox("txtChoice-" &
Convert.ToString(IDArray.Item(x)), 140, Request.Form.Item("txtChoice-"
& Convert.ToString(IDArray.Item(x)))))
Dim cell2 As New TableCell
cell2.Controls.Add(BuildDropDownList("ddlFamily-" &
Convert.ToString(IDArray.Item(x)), 150, "Family",
Request.Form.Item("ddlFamily-" & Convert.ToString(IDArray.Item(x)))))
Dim cell3 As New TableCell
cell3.Controls.Add(BuildDropDownList("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)), 150, "Attributes",
Request.Form.Item("ddlAttribute-" &
Convert.ToString(IDArray.Item(x)))))
Dim cell4 As New TableCell
cell4.Controls.Add(BuildTextBox("txtScore-" &
Convert.ToString(IDArray.Item(x)), 40, Request.Form.Item("txtScore-" &
Convert.ToString(IDArray.Item(x)))))
Dim cell5 As New TableCell
cell5.Controls.Add(BuildLinkButton("lnkDelete-" &
Convert.ToString(IDArray.Item(x))))
row.Cells.Add(cell1)
row.Cells.Add(cell2)
row.Cells.Add(cell3)
row.Cells.Add(cell4)
row.Cells.Add(cell5)
tblAnswers.Rows.Add(row)
Next
plhDynControls.Controls.Add(tblAnswers)
End Sub
--
Let me know if seeing anything else might help. Thanks again.Amoril
You cannot use NewGuid function for ids because it'll generate different id
on every call (it means also on every postback) so events for all dynamicall
y
created controls will not be fired. And you want be able to find a value
entered by the user. Use x (loop counter) with contact prefix instead. Have
also in mind you should recreate controls in page_init (but do not access
viewstate at this stage because it’s simply not collected yet) as they wil
l
automatically recreate their state.
Hope it helps
"Amoril" wrote:

> I've read quite a few different message on various boards and for some
> reason I'm still having trouble wrapping my head around this viewstate
> maintenance and trying to get these dynamically created link buttons
> to stay wired up to their click events.
> I have what is basically a simply survey question generation page. The
> page first displays a few static fields and a dropdownlist of various
> options for the user to select. When the user selects an option from
> the list the page will generate a new table with 5 rows of textboxes,
> drop down lists, and link buttons (to delete a row if desired). There
> is also a static insert button to allow users to add additional rows
> if needed.
> Saving the data in the fields during postback isn't an issue, but I'm
> stuck in two situations depending on how I adjust the code. First is
> that I put the rebuilding of the controls in the Page_load and users
> are forced to click twice on the static Insert Row button to add a row
> or they have to click twice on a dynamic Delete Row link button to
> remove a row. If I take the rebuilding of the controls out of the
> Page_Load then the Insert Row button works fine, but clicking on a
> Delete Row link button causes the click event to not fire and all the
> dynamic controls disappear from the page.
> Does anyone have any suggestions on what I need to do to fix this so
> it's written correctly and will operate as intended? (if you need more
> detail or code please ask)
> Thank you for your help.
> --Code Snippets (this setup requires 2 clicks on a button before the
> click event appears to do anything--
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> If Not IsPostBack Then
> LoadQuestionTypes()
> End If
> RebuildControls()
> End Sub
> --
> Private Sub ddlQuestionType_SelectedIndexChanged(ByV
al sender As
> System.Object, ByVal e As System.EventArgs) Handles
> ddlQuestionType.SelectedIndexChanged
> ...
> BuildEmptyFive()
> ...
> End Sub
> --
> Private Sub BuildEmptyFive()
> Dim IDArray As New ArrayList
> Dim tblAnswers As New Table
> Dim x As Integer
> For x = 1 To 5
> Dim row As New TableRow
> Dim ID As String
> ID = Left(System.Guid.NewGuid.ToString, 8)
> Dim cell1 As New TableCell
> cell1.Controls.Add(BuildTextBox("txtChoice-" & ID, 140))
> Dim cell2 As New TableCell
> cell2.Controls.Add(BuildDropDownList("ddlFamily-" & ID, 150,
> "Family"))
> Dim cell3 As New TableCell
> cell3.Controls.Add(BuildDropDownList("ddlAttribute-" & ID, 150,
> "Attributes"))
> Dim cell4 As New TableCell
> cell4.Controls.Add(BuildTextBox("txtScore-" & ID, 40))
> Dim cell5 As New TableCell
> cell5.Controls.Add(BuildLinkButton("lnkDelete-" & ID))
> row.Cells.Add(cell1)
> row.Cells.Add(cell2)
> row.Cells.Add(cell3)
> row.Cells.Add(cell4)
> row.Cells.Add(cell5)
> tblAnswers.Rows.Add(row)
> IDArray.Add(ID)
> Next
> plhDynControls.Controls.Add(tblAnswers)
> 'Insert Array containing ID of each row
> If IsNothing(ViewState.Item("IDArray")) Then
> ViewState.Add("IDArray", IDArray)
> Else
> ViewState.Item("IDArray") = IDArray
> End If
> End Sub
> --
> Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnInsert.Click
> 'Add a new ID to the viewstate which will cause a new row to be
> inserted when the viewstate is rebuilt
> Dim IDArray As ArrayList
> IDArray = CType(ViewState.Item("IDArray"), ArrayList)
> IDArray.Add(Left(Guid.NewGuid.ToString, 8))
> ViewState.Item("IDArray") = IDArray
> 'RebuildControls() 'unremark this and remove from page_load to get
> insert button to work perfectly (delete no workie though)
> End If
> End Sub
> --
> Private Sub lnkDelete_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> Dim IDArray As ArrayList
> IDArray = CType(ViewState.Item("IDArray"), ArrayList)
> Dim ID As String = Right(CType(sender, LinkButton).ID.ToString, 8)
> IDArray.RemoveAt(IDArray.IndexOf(ID))
> ViewState.Item("IDArray") = IDArray
> End Sub
> --
> this is how I generate the link button dynamically
> Private Function BuildLinkButton(ByVal name As String) As LinkButton
> Dim lnkLink As New LinkButton
> lnkLink.ID = name
> lnkLink.Text = "Delete"
> AddHandler lnkLink.Click, AddressOf lnkDelete_Click
> Return lnkLink
> End Function
> --
> Private Sub RebuildControls()
> If IsNothing(ViewState.Item("IDArray")) Then
> Exit Sub
> End If
> Dim IDArray As ArrayList
> IDArray = CType(ViewState.Item("IDArray"), ArrayList)
> Dim tblAnswers As New Table
> Dim x As Integer
> For x = 0 To IDArray.Count - 1
> Dim row As New TableRow
> Dim cell1 As New TableCell
> cell1.Controls.Add(BuildTextBox("txtChoice-" &
> Convert.ToString(IDArray.Item(x)), 140, Request.Form.Item("txtChoice-"
> & Convert.ToString(IDArray.Item(x)))))
> Dim cell2 As New TableCell
> cell2.Controls.Add(BuildDropDownList("ddlFamily-" &
> Convert.ToString(IDArray.Item(x)), 150, "Family",
> Request.Form.Item("ddlFamily-" & Convert.ToString(IDArray.Item(x)))))
> Dim cell3 As New TableCell
> cell3.Controls.Add(BuildDropDownList("ddlAttribute-" &
> Convert.ToString(IDArray.Item(x)), 150, "Attributes",
> Request.Form.Item("ddlAttribute-" &
> Convert.ToString(IDArray.Item(x)))))
> Dim cell4 As New TableCell
> cell4.Controls.Add(BuildTextBox("txtScore-" &
> Convert.ToString(IDArray.Item(x)), 40, Request.Form.Item("txtScore-" &
> Convert.ToString(IDArray.Item(x)))))
> Dim cell5 As New TableCell
> cell5.Controls.Add(BuildLinkButton("lnkDelete-" &
> Convert.ToString(IDArray.Item(x))))
> row.Cells.Add(cell1)
> row.Cells.Add(cell2)
> row.Cells.Add(cell3)
> row.Cells.Add(cell4)
> row.Cells.Add(cell5)
> tblAnswers.Rows.Add(row)
> Next
> plhDynControls.Controls.Add(tblAnswers)
> End Sub
> --
> Let me know if seeing anything else might help. Thanks again.
>
The only place that I use NewGuid to assign the ID's is in the
BuildEmptyFive sub (only fired after the user selects an item from the
drop down), for RebuildingControls sub I pull the ID's out of the
IDArray in the ViewState, so that shouldn't be an issue.
Moving the RebuildControls() sub from Page_Load to Page_Init actually
made the issue worse, now when I click on the static Insert Row button
or the dynamics link buttons to delete a row, all the dynamic controls
disappear. The static button fires it's event, but the link buttons
don't. Perhaps I'm not understanding what you mean by that since
without accessing the IDArray in the viewstate I won't know how many
controls need to be recreated.
Any more detail you could provide would be appreciated.
Hi again,
Oh yes, you're right but no need for that. it's easier to use row index and
a constant prefix for a particular control type (attribute, score,etc). I'll
try to provide a fully working example later on today.
take care
--
Milosz
"Amoril" wrote:

> The only place that I use NewGuid to assign the ID's is in the
> BuildEmptyFive sub (only fired after the user selects an item from the
> drop down), for RebuildingControls sub I pull the ID's out of the
> IDArray in the ViewState, so that shouldn't be an issue.
> Moving the RebuildControls() sub from Page_Load to Page_Init actually
> made the issue worse, now when I click on the static Insert Row button
> or the dynamics link buttons to delete a row, all the dynamic controls
> disappear. The static button fires it's event, but the link buttons
> don't. Perhaps I'm not understanding what you mean by that since
> without accessing the IDArray in the viewstate I won't know how many
> controls need to be recreated.
> Any more detail you could provide would be appreciated.
>
Hi again,
Actually we have to use guid because you can delete row, which i didn't pick
up before. Anyway, i created fully working example for you. You should be
fine from this point
-- begin aspx code --
<%@. Page Language="VB" AutoEventWireup="false" CodeFile="Survey.aspx.vb"
Inherits="Survey" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="questions" AutoPostBack="true">
<asp:ListItem Text="Please Select a Question..." />
<asp:ListItem Text="What are your names?" />
<asp:ListItem Text="Name all girlfriends you have had in your life" />
</asp:DropDownList>
<asp:Panel runat="server" ID="container" />
<asp:Panel runat="server" ID="surveyOptions">
<asp:Button ID="btnAddRow" runat="server" Text="Add row" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit Survey"/>
</asp:Panel>
</div>
</form>
</body>
</html>
-- end aspx code --
-- begin vb.net code --
Partial Class Survey
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
RecreateRows()
End Sub
Protected Sub questions_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles questions.SelectedIndexChanged
Const DefaultRowCount As Integer = 5
' clear everything
IDs.Clear()
If CType(sender, DropDownList).SelectedIndex >= 0 Then
' create x default empty rows
For i As Integer = 1 To DefaultRowCount
IDs.Add(GenerateId())
Next
End If
RecreateRows()
End Sub
Private Sub RecreateRows()
container.Controls.Clear()
For Each id As String In IDs
AddAnswerRow(id)
Next
surveyOptions.Visible = IDs.Count > 0
End Sub
Private Const RowIdPrefix As String = "row"
Private Const TextBoxIdPrefix As String = "txt"
Private Const DropDownListIdPrefix As String = "ddl"
Private Sub AddAnswerRow(ByVal id As String)
Dim panel As Panel
Dim textBox As TextBox
Dim linkButton As LinkButton
Dim dropDownList As DropDownList
' row panel
panel = New Panel()
panel.ID = RowIdPrefix & id
' answer text box
textBox = New TextBox()
textBox.ID = TextBoxIdPrefix & id
' delete button
linkButton = New LinkButton()
linkButton.ID = "btn" & id
linkButton.Text = "delete"
linkButton.CommandArgument = id
AddHandler linkButton.Command, New CommandEventHandler(AddressOf
DeleteAnswerRow)
dropDownList = New DropDownList()
dropDownList.ID = DropDownListIdPrefix & id
dropDownList.Items.Add(New ListItem("Value0", "0"))
dropDownList.Items.Add(New ListItem("Value1", "1"))
dropDownList.Items.Add(New ListItem("Value2", "2"))
panel.Controls.Add(textBox)
panel.Controls.Add(dropDownList)
panel.Controls.Add(linkButton)
container.Controls.Add(panel)
End Sub
Private Sub DeleteAnswerRow(ByVal source As Object, ByVal e As
CommandEventArgs)
Dim id As String = CType(e.CommandArgument, String)
Dim control As Control = container.FindControl(RowIdPrefix & id)
If (Not control Is Nothing) Then
container.Controls.Remove(control)
Dim index As Integer = IDs.IndexOf(id)
If index <> -1 Then
IDs.RemoveAt(index)
End If
End If
End Sub
Private ReadOnly Property IDs() As ArrayList
Get
Dim value As Object = ViewState("IDs")
If value Is Nothing Then
value = New ArrayList()
ViewState("IDs") = value
End If
Return value
End Get
End Property
Private Function GenerateId() As String
Return Guid.NewGuid().ToString("N")
End Function
Protected Sub btnAddRow_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAddRow.Click
Dim id As String = GenerateId()
IDs.Add(id)
AddAnswerRow(id)
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
' obtain results
Dim textBox As TextBox
Dim dropDownList As DropDownList
For Each id As String In IDs
'
' text box value
'
textBox = CType(container.FindControl(TextBoxIdPrefix & id), TextBox)
If (Not textBox Is Nothing) Then
Dim textBoxValue As String = textBox.Text
End If
'
' drop down list selected value
'
dropDownList = CType(container.FindControl(DropDownListIdPrefix & id),
DropDownList)
If (Not dropDownList Is Nothing) Then
Dim dropDownListValue As String = dropDownList.SelectedValue
End If
Next
End Sub
End Class
-- end vb.net code --
Milosz
"Milosz Skalecki [MCAD]" wrote:
> Hi again,
> Oh yes, you're right but no need for that. it's easier to use row index an
d
> a constant prefix for a particular control type (attribute, score,etc). I'
ll
> try to provide a fully working example later on today.
> take care
> --
> Milosz
>
> "Amoril" wrote:
>
Excellent, thank you very much for your help, it's working great.

Thursday, March 22, 2012

Trouble with OleDb data extract

Hi,

I have an unusual problem that just showed its ugly head at a pretty
bad time. I have an asp.net (VB) app that takes data from an Excel
sheet and puts it into SQL Server. I get the data out of Excel using
OleDB, and suddenly, some of the data was not being extracted from
Excel.

I use OleDb for the extract into a DataTable and from there an
SqlClient.SqlCommand to put it into SQL Server.

I put the results of the OleDb extract into a datagrid to see if the
problem was there or the SqlClient insert. The datagrid showed
missing data, even before I got to the SQL insert. Here's my code
(roughly):

Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
_
& "Data Source = " test.xls" _
& ";" & "Extended Properties=Excel 8.0;"
Dim objConnXL As New OleDbConnection(sConnectionString)

''''Create Data Adapter and Data Set.
Dim Employee Select As New OleDbCommand("SELECT * FROM
Employee where RowNum > 0 ", objConnXL)
Dim Employee Adapter As New OleDbDataAdapter()
Employee Adapter.SelectCommand = Employee Select
Dim Employee Dataset As New DataSet()
Employee Adapter.Fill(Employee Dataset, "XLData")

'DataGrid1.DataSource = Employee Dataset.Tables(0).DefaultView
'DataGrid1.DataBind()

Assume the following data from an Excel namespace called Employee:

FirstLastAddrCityStateZipRowNum
JoeSmith123 Main NYNY123451
BillJones456 NorthLACA543212

The extract suddenly omitted the zip 12345, but displayed the other 13
pieces of data fine. I can see the data in the Excel sheet. This has
been working fine for several months, and of course, last week we went
live, go figure.

If anyone has run into this or anything like it, please give a yell.

Thanks.Hi Brian,

I am not sure about the exact meaning of "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine." As I
understand, you mean that you lost the first row. Please remove "where
RowNum > 0" and test this issue again.

In addition, I believe that the following article is useful to you. Please
refer to it carefully.

Read Excel files from ASP.NET
http://www.aspfree.com/examples/766,1/examples.aspx
"...
This page provides a simple example of how to query an Excel spreadsheet
from an ASP.NET page using either C# or VB.NET.
..."

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hi Brian,

I am not sure about the exact meaning of "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine." As I
understand, you mean that you lost the first row. Please remove "where
RowNum > 0" and test this issue again.

In addition, I believe that the following article is useful to you. Please
refer to it carefully.

Read Excel files from ASP.NET
http://www.aspfree.com/examples/766,1/examples.aspx
"...
This page provides a simple example of how to query an Excel spreadsheet
from an ASP.NET page using either C# or VB.NET.
..."

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Hi Jacob,

Thanks for the reply. What I meant by "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine is this.

Assume the Excel data looking like this:

FirstLastAddrCityStateZipRowNum
JoeSmith123 Main NYNY123451
BillJones456 NorthLACA543212

Then assume the datagrid that selects everything from the Excel sheet
looks like this:

FirstLastAddrCityStateZipRowNum
JoeSmith123 Main NYNY1
BillJones456 NorthLACA543212

All data is selected from the Excel sheet except for one field.

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Hi Jacob,

Thanks for the reply. What I meant by "The extract suddenly omitted the
zip 12345, but displayed the other 13 pieces of data fine is this.

Assume the Excel data looking like this:

FirstLastAddrCityStateZipRowNum
JoeSmith123 Main NYNY123451
BillJones456 NorthLACA543212

Then assume the datagrid that selects everything from the Excel sheet
looks like this:

FirstLastAddrCityStateZipRowNum
JoeSmith123 Main NYNY1
BillJones456 NorthLACA543212

All data is selected from the Excel sheet except for one field.

Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
I assume by 'Excel namespace' you mean 'defined name' or 'named
range'. Have you checked its definition to ensure it is still
referencing the same Excel range i.e. including the header row?

BTW you should, for all sorts of reasons, seriously consider changing
your SQL from

SELECT * FROM ...

to

SELECT First, Last, Addr, City, State, Zip, RowNum FROM ...

You never know, it may help with the current problem.

--

bdhanson@.mcdermott.com (Brian Hanson) wrote in message news:<169b400f.0312031839.31617180@.posting.google.com>...
> Hi,
> I have an unusual problem that just showed its ugly head at a pretty
> bad time. I have an asp.net (VB) app that takes data from an Excel
> sheet and puts it into SQL Server. I get the data out of Excel using
> OleDB, and suddenly, some of the data was not being extracted from
> Excel.
> I use OleDb for the extract into a DataTable and from there an
> SqlClient.SqlCommand to put it into SQL Server.
> I put the results of the OleDb extract into a datagrid to see if the
> problem was there or the SqlClient insert. The datagrid showed
> missing data, even before I got to the SQL insert. Here's my code
> (roughly):
> Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
> _
> & "Data Source = " test.xls" _
> & ";" & "Extended Properties=Excel 8.0;"
> Dim objConnXL As New OleDbConnection(sConnectionString)
> ''''Create Data Adapter and Data Set.
> Dim Employee Select As New OleDbCommand("SELECT * FROM
> Employee where RowNum > 0 ", objConnXL)
> Dim Employee Adapter As New OleDbDataAdapter()
> Employee Adapter.SelectCommand = Employee Select
> Dim Employee Dataset As New DataSet()
> Employee Adapter.Fill(Employee Dataset, "XLData")
> 'DataGrid1.DataSource = Employee Dataset.Tables(0).DefaultView
> 'DataGrid1.DataBind()
>
> Assume the following data from an Excel namespace called Employee:
> FirstLastAddrCityStateZipRowNum
> JoeSmith123 Main NYNY123451
> BillJones456 NorthLACA543212
>
> The extract suddenly omitted the zip 12345, but displayed the other 13
> pieces of data fine. I can see the data in the Excel sheet. This has
> been working fine for several months, and of course, last week we went
> live, go figure.
> If anyone has run into this or anything like it, please give a yell.
> Thanks.
Thank you both for your replies, heres what Ive learned in the last
few hours. First, by Excel namespace I did mean the named range, and it
has not changed. Also, I was using column names in my select vs. the
select * method.

What I have learned is the sequence of events while the user fills out
the Excel form is what triggers this, but I cant assume that this is
the whole story. The column in question is originally set to datatype
Text. It may be a number, but is displayed as entered. We have a
button that executes the Cell.Clear function in VBA code. This was the
problem. It changed the column datatype from Text to General (and
possibly done more than that) now the OleDb extract will not read that
data.

We have fixed the problem going forward by using Cell.ClearContents
instead of Cell.Clear, but we still have existing Excel forms that I
need to read data from. I have manually switched the datatype from
General back to Text, but it still will not read the data. I have tried
to do a conversion from within the OleDb select statement, but it says
the value I am trying to convert is null gives me an error.

One other weird thing that I have noticed but am not sure is important.
When the data is originally entered into a field of type Text, it is
right justified in the cell. Once I do the Cell.Clear and it changes to
General data type, the data is left justified in the cell. I then
change the type back to Text, but the data remains left justified.
Final result, I still cannot get the data out of existing Excel sheets
via the OleDbCommand.

Thanks again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Hi Brian,

Thank you for your update. It seems to be a strange issue.

Have you tested the example I mentioned before?

Read Excel files from ASP.NET
http://www.aspfree.com/examples/766,1/examples.aspx
"...
This page provides a simple example of how to query an Excel spreadsheet
from an ASP.NET page using either C# or VB.NET.
..."

Can you reproduce the same problem the above sample? I certainly appreciate
your time.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hi,

FYI, we got our prob fixed & I hope this doesn't show twice. Setting
the IMEX =1 property of the connection string forced the data to be read
as text. Actually, it forces this registry setting:

'
hkey_local_machine|software\microsoft\jet\4.0\engi nes\excel\ImportMixedT
ypes.

It tells Excel how to treat mixed datatypes and was already set to text.
Apparently, the IMEX setting enforces the registry setting.

Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"_
& "Data Source = " test.xls" _
& ";" & "Extended Properties=""Excel 8.0; IMEX=1; """

That alone was giving an error of Could not find installable ISAM. It
was not untill the extra were added to the extended properties
section that it worked.

This was the post that worked for me:

http://groups.google.com/groups?hl=...-8&threadm=ebql
3v8ie02vcld5j7adc3p3pj9j3c0tp5%404ax.com&rnum=2&prev=/groups%3Fq%3Dimex%
2B%2B%2BCould%2Bnot%2Bfind%2Binstallable%2BISAM%2B oledb%26ie%3DUTF-8%26o
e%3DUTF-8%26hl%3Den%26btnG%3DGoogle%2BSearch

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Hi Brian,

Thank you very much for sharing your solution. It is helpful to everybody
here.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.