Monday, March 26, 2012
Populate datagrid from XML file using where type query
following:
Try
Dim ds As New DataSet
ds.ReadXml(New
StringReader(HttpContext.Current.Application("var").ToString))
dataMeet.DataSource = ds
dataMeet.DataBind()
Catch ex As Exception
error_email.fncErrorHandler(ex)
lblError.Text = ex.ToString
End Try
however this does not allow me to use a sql type query to select
exactly the data I want in the datagrid.
The query I am looking to achieve is actually very simple, just a
simple 'where' but I have no idea how to achieve this when binding to
an xml variable
The sql query and a sample xml file are pasted below:
select TITLE, FIRST_NAME || ' ' || LAST_NAME AS FULL_NAME, DEPT, EMAIL,
PHONE FROM DATABASE.TABLE "
WHERE MEMBERTYPE_CODE = 'MEM' ORDER BY MEMBER_ID
<NewDataSet>
<Table>
<MEMBERTYPE_CODE>OFF</MEMBERTYPE_CODE>
<LAST_NAME>Smith</LAST_NAME>
<FIRST_NAME>Harry</FIRST_NAME>
<TITLE>engineer</TITLE>
<DEPT>engineering</DEPT>
<EMAIL>harry.smith@dotnet.itags.org.email.address</EMAIL>
<PHONE>(111) 111-1111</PHONE>
</Table>
<Table>
<MEMBERTYPE_CODE>MEM</MEMBERTYPE_CODE>
<LAST_NAME>fred</LAST_NAME>
<FIRST_NAME>bloggs</FIRST_NAME>
<TITLE>Webmaster</TITLE>
<DEPT>Information Technology Services</DEPT>
<EMAIL>fred.blogs@dotnet.itags.org.email.address</EMAIL>
<PHONE>(111) 111-1112</PHONE>
</Table>
</NewDataSet>
Thank you for your timePerfect, just what I needed!
Thank you
Populate datagrid from XML file using where type query
following:
Try
Dim ds As New DataSet
ds.ReadXml(New
StringReader(HttpContext.Current.Application("var").ToString))
dataMeet.DataSource = ds
dataMeet.DataBind()
Catch ex As Exception
error_email.fncErrorHandler(ex)
lblError.Text = ex.ToString
End Try
however this does not allow me to use a sql type query to select
exactly the data I want in the datagrid.
The query I am looking to achieve is actually very simple, just a
simple 'where' but I have no idea how to achieve this when binding to
an xml variable
The sql query and a sample xml file are pasted below:
select TITLE, FIRST_NAME || ' ' || LAST_NAME AS FULL_NAME, DEPT, EMAIL,
PHONE FROM DATABASE.TABLE "
WHERE MEMBERTYPE_CODE = 'MEM' ORDER BY MEMBER_ID
<NewDataSet>
<Table>
<MEMBERTYPE_CODE>OFF</MEMBERTYPE_CODE>
<LAST_NAME>Smith</LAST_NAME>
<FIRST_NAME>Harry</FIRST_NAME>
<TITLE>engineer</TITLE>
<DEPT>engineering</DEPT>
<EMAIL>harry.smith@dotnet.itags.org.email.address</EMAIL>
<PHONE>(111) 111-1111</PHONE>
</Table>
<Table>
<MEMBERTYPE_CODE>MEM</MEMBERTYPE_CODE>
<LAST_NAME>fred</LAST_NAME>
<FIRST_NAME>bloggs</FIRST_NAME>
<TITLE>Webmaster</TITLE>
<DEPT>Information Technology Services</DEPT>
<EMAIL>fred.blogs@dotnet.itags.org.email.address</EMAIL>
<PHONE>(111) 111-1112</PHONE>
</Table>
</NewDataSet
Thank you for your timeHi gn,
You can use DataView's properties RowFilter and Sort to
perform functionality similar to 'WHERE' condition
and 'ORDER BY' in sql query. Following is sample code
snippet:
Dim dv As DataView = ds.Tables(0).DefaultView
dv.RowFilter = "MEMBERTYPE_CODE = 'MEM'"
dv.Sort = "LAST_NAME"
dataMeet.DataSource = dv
dataMeet.DataBind()
HTH
Elton Wang
elton_wang@.hotmail.com
>--Original Message--
>I am able to populate a datagrid from an XML variable
using the
>following:
> Try
> Dim ds As New DataSet
> ds.ReadXml(New
>StringReader(HttpContext.Current.Application
("var").ToString))
> dataMeet.DataSource = ds
> dataMeet.DataBind()
> Catch ex As Exception
> error_email.fncErrorHandler(ex)
> lblError.Text = ex.ToString
> End Try
>however this does not allow me to use a sql type query to
select
>exactly the data I want in the datagrid.
>The query I am looking to achieve is actually very
simple, just a
>simple 'where' but I have no idea how to achieve this
when binding to
>an xml variable
>The sql query and a sample xml file are pasted below:
>select TITLE, FIRST_NAME || ' ' || LAST_NAME AS
FULL_NAME, DEPT, EMAIL,
>PHONE FROM DATABASE.TABLE "
>WHERE MEMBERTYPE_CODE = 'MEM' ORDER BY MEMBER_ID
><NewDataSet>
> <Table>
> <MEMBERTYPE_CODE>OFF</MEMBERTYPE_CODE>
> <LAST_NAME>Smith</LAST_NAME>
> <FIRST_NAME>Harry</FIRST_NAME>
> <TITLE>engineer</TITLE>
> <DEPT>engineering</DEPT>
> <EMAIL>harry.smith@.email.address</EMAIL>
> <PHONE>(111) 111-1111</PHONE>
> </Table>
> <Table>
> <MEMBERTYPE_CODE>MEM</MEMBERTYPE_CODE>
> <LAST_NAME>fred</LAST_NAME>
> <FIRST_NAME>bloggs</FIRST_NAME>
> <TITLE>Webmaster</TITLE>
> <DEPT>Information Technology Services</DEPT>
> <EMAIL>fred.blogs@.email.address</EMAIL>
> <PHONE>(111) 111-1112</PHONE>
> </Table>
></NewDataSet>
>Thank you for your time
>.
Perfect, just what I needed!
Thank you
Saturday, March 24, 2012
Populate dropdown list with XML
I need to populate the ID and the value of the record.
Code sample appreciated...thanksPlease see below code, that helps for your question.
Here is the simple XML file, I am using to bind to dropdownlist and name is as Xmlfile.xml
<?xml version="1.0" encoding="utf-8" ?>
<DataList>
<Student>
<Name>Creator</Name >
<Roll>101</Roll >
</Student >
<Student>
<Name>Maintainer</Name >
<Roll>102</Roll >
</Student
</DataList>
and I am using this file to bind with Dropdownlost in page load event. Assuming DropDownList server control name is DropDownList1
Dim Ds As New System.Data.DataSet
Ds.ReadXml("D:\WebSites\VS2005First\XMLFile.xml")DropDownList1.DataSource = Ds.Tables(0).DefaultView
DropDownList1.DataTextField = "Roll"
DropDownList1.DataValueField = "Name"
DropDownList1.DataBind()
Hope this helps you!
ok that is pretty easy,fist of all u need to populate ur xml data to your datagrid then bind it to ur dropdownlist.
xml file
<root>
<userName>Bob</userName>
<userID>1</userID>
</root
vb.net
'read your data from your datagrid
'assuming ur xml is your root directory
'
Dim ds As DataSet
ds.ReadXml(Server.MapPath("file.xml"))
'bind your dataset to your dropdown list
DropDownList1.DataSource = ds
'assign the element of ur xml to the ddl
DropDownList1.DataTextField = "userName"
DropDownList1.DataValueField = "userID"
DropDownList1.DataBind()
hope that help.Happy coding
Thank you all for replying - I will try the suggestions given
Populate Dropdown with File Names
populate a Dropdown with the File Names that reside in an image folder
that's in the same project. So far all I've been able to find is the
below code...and even then that's not quite what I'm after.
DirectoryInfo di = new DirectoryInfo(@dotnet.itags.org."~/Images/Entry/");
FileInfo[] fis = di.GetFiles();
DropDownList5.DataSource = fis;
DropDownList5.DataTextField = "Name";
DropDownList5.DataValueField = "Name";
DropDownList5.DataBind();
Any help in trying to get this resolved quickly would be appreciated.
ThanksOne thing I neglected to say...the Dropdown will also reside in the
EditItemTemplate of a Formview. Is there a way to bind that control if
it's there?
Thanks Again
Dave wrote:
Quote:
Originally Posted by
I'm working with an ASP.Net web page that's using C#. I'm trying to
populate a Dropdown with the File Names that reside in an image folder
that's in the same project. So far all I've been able to find is the
below code...and even then that's not quite what I'm after.
>
DirectoryInfo di = new DirectoryInfo(@."~/Images/Entry/");
FileInfo[] fis = di.GetFiles();
DropDownList5.DataSource = fis;
DropDownList5.DataTextField = "Name";
DropDownList5.DataValueField = "Name";
DropDownList5.DataBind();
>
Any help in trying to get this resolved quickly would be appreciated.
>
Thanks
Wednesday, March 21, 2012
Populate TextBox With DB Records
a SqlDataReader to the calling function which exists in a ASPX page:
Namespace NConnect
Public Class Cart
Private sqlConn As New SqlConnection("....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Try
With sqlCmd
.Parameters.Add("@dotnet.itags.org.UserID", SqlDbType.Int).Value =
UserID
End With
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
Catch ex As Exception
Throw ex
End Try
Return sqlReader
End Function
End Class
End Namespace
Using vbc, I successfully compiled the above class file into a DLL
named NConnect.dll.
This is the simple stored procedure:
CREATE PROCEDURE NETGetAddress
@dotnet.itags.org.UserID integer
AS
SELECT UserID, Address, City, State, Country, Zip FROM tblUsers WHERE
UserID = @dotnet.itags.org.UserID
The ASPX page uses a user control named 'Address.ascx' which has 5
TextBoxes named txtAddress, txtCity, txtState, txtCountry & txtZip. I
am using the Get & Set statement for creating a property for each of
the TextBoxes like this:
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
'here comes the other properties
Finally, this is the ASPX page:
<%@dotnet.itags.org. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@dotnet.itags.org. Import Namespace="NConnect" %>
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
'retrieving the UserID by calling another function
sqlReader = boCart.GetAddress(iUserID)
End Sub
</script>
<form runat="server">
<NETConnect:NETAddress ID="ncBillingAddress" runat="server"/>
</form>
The ASPX page will render the 5 TextBoxes. Now how do I populate the 5
TextBoxes with the records that the stored procedure retrieves for a
particular UserID?OK....this is what I tried in the ASPX page (this ASPX page is named
NETAddress.aspx):
<%@. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@. Import Namespace="NConnect" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
Dim nAddress As netaddress_ascx
'retrieving the UserID by calling another function
nAddress = Page.LoadControl("NETAddress.ascx")
nAddress.ID = "ncBilling"
pnlAddress.Controls.Add(nAddress)
sqlReader = boCart.GetAddress(iUserID)
Response.Write("Field Count: " & sqlReader.FieldCount)
nAddress.Address = sqlReader.GetString(1)
nAddress.City = sqlReader.GetString(2)
nAddress.StateName = sqlReader.GetString(3)
nAddress.Country = sqlReader.GetString(4)
nAddress.Zip = sqlReader.GetString(5)
End Sub
</script>
<form runat="server">
<asp:Panel ID="pnlAddress" runat="server"/>
</form>
But the above generates this error:
Invalid attempt to read when no data is present.
pointing to
nAddress.Address = sqlReader.GetString(1)
What's causing the error? Note the Response.Write(sqlReader.FieldCount)
line. If I comment out all the GetString lines after this line, then
sqlReader.FieldCount correctly displays the field count as 6. So how
come sqlReader.GetString(1) has no data?
Moreover if I bind the data to a DataList, then also the DataList
renders all the records corresponding to the UserID; so where am I
going wrong?
rn5a@.rediffmail.com wrote:
> This function in a VB class file takes UserID as a parameter & returns
> a SqlDataReader to the calling function which exists in a ASPX page:
> Namespace NConnect
> Public Class Cart
> Private sqlConn As New SqlConnection("....")
> Public Function GetAddress(ByVal UserID As Integer) As
> SqlDataReader
> Dim sqlCmd As SqlCommand
> Dim sqlReader As SqlDataReader
> sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
> sqlCmd.CommandType = CommandType.StoredProcedure
> Try
> With sqlCmd
> .Parameters.Add("@.UserID", SqlDbType.Int).Value =
> UserID
> End With
> sqlConn.Open()
> sqlReader = sqlCmd.ExecuteReader
> Catch ex As Exception
> Throw ex
> End Try
> Return sqlReader
> End Function
> End Class
> End Namespace
> Using vbc, I successfully compiled the above class file into a DLL
> named NConnect.dll.
> This is the simple stored procedure:
> CREATE PROCEDURE NETGetAddress
> @.UserID integer
> AS
> SELECT UserID, Address, City, State, Country, Zip FROM tblUsers WHERE
> UserID = @.UserID
> The ASPX page uses a user control named 'Address.ascx' which has 5
> TextBoxes named txtAddress, txtCity, txtState, txtCountry & txtZip. I
> am using the Get & Set statement for creating a property for each of
> the TextBoxes like this:
> Public Property Address() As String
> Get
> Address = txtAddress.Text
> End Get
> Set(ByVal value As String)
> txtAddress.Text = value
> End Set
> End Property
> 'here comes the other properties
> Finally, this is the ASPX page:
> <%@. Register TagPrefix="NETConnect" TagName="Address"
> src="http://pics.10026.com/?src=NETAddress.ascx" %>
> <%@. Import Namespace="NConnect" %>
> <%@. Import Namespace="System.Data" %>
> <%@. Import Namespace="System.Data.SqlClient" %>
> <script runat="server">
> Sub Page_Load(....)
> Dim boCart As Cart
> Dim iUserID As Integer
> Dim sqlReader As SqlDataReader
> 'retrieving the UserID by calling another function
> sqlReader = boCart.GetAddress(iUserID)
> End Sub
> </script>
> <form runat="server">
> <NETConnect:NETAddress ID="ncBillingAddress" runat="server"/>
> </form>
> The ASPX page will render the 5 TextBoxes. Now how do I populate the 5
> TextBoxes with the records that the stored procedure retrieves for a
> particular UserID?
Well...the mistake I was making was I wasn't looping through the
SqlDataReader using the Read method of the SqlDataReader...
While(sqlReader.Read)
nAddress.Address = sqlReader.GetString(1)
nAddress.City = sqlReader.GetString(2)
nAddress.StateName = sqlReader.GetString(3)
nAddress.Country = sqlReader.GetString(4)
nAddress.Zip = sqlReader.GetString(5)
End While
rn5a@.rediffmail.com wrote:
> OK....this is what I tried in the ASPX page (this ASPX page is named
> NETAddress.aspx):
> <%@. Register TagPrefix="NETConnect" TagName="Address"
> src="http://pics.10026.com/?src=NETAddress.ascx" %>
> <%@. Import Namespace="NConnect" %>
> <%@. Import Namespace="System.Data" %>
> <%@. Import Namespace="System.Data.SqlClient" %>
> <script runat="server">
> Sub Page_Load(....)
> Dim boCart As Cart
> Dim iUserID As Integer
> Dim sqlReader As SqlDataReader
> Dim nAddress As netaddress_ascx
> 'retrieving the UserID by calling another function
> nAddress = Page.LoadControl("NETAddress.ascx")
> nAddress.ID = "ncBilling"
> pnlAddress.Controls.Add(nAddress)
> sqlReader = boCart.GetAddress(iUserID)
> Response.Write("Field Count: " & sqlReader.FieldCount)
> nAddress.Address = sqlReader.GetString(1)
> nAddress.City = sqlReader.GetString(2)
> nAddress.StateName = sqlReader.GetString(3)
> nAddress.Country = sqlReader.GetString(4)
> nAddress.Zip = sqlReader.GetString(5)
> End Sub
> </script>
> <form runat="server">
> <asp:Panel ID="pnlAddress" runat="server"/>
> </form>
> But the above generates this error:
> Invalid attempt to read when no data is present.
> pointing to
> nAddress.Address = sqlReader.GetString(1)
> What's causing the error? Note the Response.Write(sqlReader.FieldCount)
> line. If I comment out all the GetString lines after this line, then
> sqlReader.FieldCount correctly displays the field count as 6. So how
> come sqlReader.GetString(1) has no data?
> Moreover if I bind the data to a DataList, then also the DataList
> renders all the records corresponding to the UserID; so where am I
> going wrong?
>
> rn5a@.rediffmail.com wrote:
Populate TextBox With DB Records
a SqlDataReader to the calling function which exists in a ASPX page:
Namespace NConnect
Public Class Cart
Private sqlConn As New SqlConnection("....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Try
With sqlCmd
.Parameters.Add("@dotnet.itags.org.UserID", SqlDbType.Int).Value =
UserID
End With
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
Catch ex As Exception
Throw ex
End Try
Return sqlReader
End Function
End Class
End Namespace
Using vbc, I successfully compiled the above class file into a DLL
named NConnect.dll.
This is the simple stored procedure:
CREATE PROCEDURE NETGetAddress
@dotnet.itags.org.UserID integer
AS
SELECT UserID, Address, City, State, Country, Zip FROM tblUsers WHERE
UserID = @dotnet.itags.org.UserID
The ASPX page uses a user control named 'Address.ascx' which has 5
TextBoxes named txtAddress, txtCity, txtState, txtCountry & txtZip. I
am using the Get & Set statement for creating a property for each of
the TextBoxes like this:
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
'here comes the other properties
Finally, this is the ASPX page:
<%@dotnet.itags.org. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@dotnet.itags.org. Import Namespace="NConnect" %>
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
'retrieving the UserID by calling another function
sqlReader = boCart.GetAddress(iUserID)
End Sub
</script>
<form runat="server">
<NETConnect:NETAddress ID="ncBillingAddress" runat="server"/>
</form>
The ASPX page will render the 5 TextBoxes. Now how do I populate the 5
TextBoxes with the records that the stored procedure retrieves for a
particular UserID?OK....this is what I tried in the ASPX page (this ASPX page is named
NETAddress.aspx):
<%@. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@. Import Namespace="NConnect" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
Dim nAddress As netaddress_ascx
'retrieving the UserID by calling another function
nAddress = Page.LoadControl("NETAddress.ascx")
nAddress.ID = "ncBilling"
pnlAddress.Controls.Add(nAddress)
sqlReader = boCart.GetAddress(iUserID)
Response.Write("Field Count: " & sqlReader.FieldCount)
nAddress.Address = sqlReader.GetString(1)
nAddress.City = sqlReader.GetString(2)
nAddress.StateName = sqlReader.GetString(3)
nAddress.Country = sqlReader.GetString(4)
nAddress.Zip = sqlReader.GetString(5)
End Sub
</script>
<form runat="server">
<asp:Panel ID="pnlAddress" runat="server"/>
</form>
But the above generates this error:
Invalid attempt to read when no data is present.
pointing to
nAddress.Address = sqlReader.GetString(1)
What's causing the error? Note the Response.Write(sqlReader.FieldCount)
line. If I comment out all the GetString lines after this line, then
sqlReader.FieldCount correctly displays the field count as 6. So how
come sqlReader.GetString(1) has no data?
Moreover if I bind the data to a DataList, then also the DataList
renders all the records corresponding to the UserID; so where am I
going wrong?
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
This function in a VB class file takes UserID as a parameter & returns
a SqlDataReader to the calling function which exists in a ASPX page:
>
Namespace NConnect
Public Class Cart
Private sqlConn As New SqlConnection("....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
>
sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
>
Try
With sqlCmd
.Parameters.Add("@.UserID", SqlDbType.Int).Value =
UserID
End With
>
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
Catch ex As Exception
Throw ex
End Try
>
Return sqlReader
End Function
End Class
End Namespace
>
Using vbc, I successfully compiled the above class file into a DLL
named NConnect.dll.
>
This is the simple stored procedure:
>
CREATE PROCEDURE NETGetAddress
@.UserID integer
AS
SELECT UserID, Address, City, State, Country, Zip FROM tblUsers WHERE
UserID = @.UserID
>
The ASPX page uses a user control named 'Address.ascx' which has 5
TextBoxes named txtAddress, txtCity, txtState, txtCountry & txtZip. I
am using the Get & Set statement for creating a property for each of
the TextBoxes like this:
>
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
>
'here comes the other properties
>
Finally, this is the ASPX page:
>
<%@. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@. Import Namespace="NConnect" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
>
'retrieving the UserID by calling another function
sqlReader = boCart.GetAddress(iUserID)
End Sub
</script>
>
<form runat="server">
<NETConnect:NETAddress ID="ncBillingAddress" runat="server"/>
</form>
>
The ASPX page will render the 5 TextBoxes. Now how do I populate the 5
TextBoxes with the records that the stored procedure retrieves for a
particular UserID?
Well...the mistake I was making was I wasn't looping through the
SqlDataReader using the Read method of the SqlDataReader...
While(sqlReader.Read)
nAddress.Address = sqlReader.GetString(1)
nAddress.City = sqlReader.GetString(2)
nAddress.StateName = sqlReader.GetString(3)
nAddress.Country = sqlReader.GetString(4)
nAddress.Zip = sqlReader.GetString(5)
End While
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
OK....this is what I tried in the ASPX page (this ASPX page is named
NETAddress.aspx):
>
<%@. Register TagPrefix="NETConnect" TagName="Address"
Src="NETAddress.ascx" %>
<%@. Import Namespace="NConnect" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
Dim nAddress As netaddress_ascx
>
'retrieving the UserID by calling another function
>
nAddress = Page.LoadControl("NETAddress.ascx")
nAddress.ID = "ncBilling"
pnlAddress.Controls.Add(nAddress)
>
sqlReader = boCart.GetAddress(iUserID)
>
Response.Write("Field Count: " & sqlReader.FieldCount)
nAddress.Address = sqlReader.GetString(1)
nAddress.City = sqlReader.GetString(2)
nAddress.StateName = sqlReader.GetString(3)
nAddress.Country = sqlReader.GetString(4)
nAddress.Zip = sqlReader.GetString(5)
End Sub
</script>
<form runat="server">
<asp:Panel ID="pnlAddress" runat="server"/>
</form>
>
But the above generates this error:
>
Invalid attempt to read when no data is present.
>
pointing to
>
nAddress.Address = sqlReader.GetString(1)
>
What's causing the error? Note the Response.Write(sqlReader.FieldCount)
line. If I comment out all the GetString lines after this line, then
sqlReader.FieldCount correctly displays the field count as 6. So how
come sqlReader.GetString(1) has no data?
>
Moreover if I bind the data to a DataList, then also the DataList
renders all the records corresponding to the UserID; so where am I
going wrong?
>
>
rn5a@.rediffmail.com wrote:
Quote:
Originally Posted by
This function in a VB class file takes UserID as a parameter & returns
a SqlDataReader to the calling function which exists in a ASPX page:
Namespace NConnect
Public Class Cart
Private sqlConn As New SqlConnection("....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
sqlCmd = New SqlCommand("NETGetAddress", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Try
With sqlCmd
.Parameters.Add("@.UserID", SqlDbType.Int).Value =
UserID
End With
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
Catch ex As Exception
Throw ex
End Try
Return sqlReader
End Function
End Class
End Namespace
Using vbc, I successfully compiled the above class file into a DLL
named NConnect.dll.
This is the simple stored procedure:
CREATE PROCEDURE NETGetAddress
@.UserID integer
AS
SELECT UserID, Address, City, State, Country, Zip FROM tblUsers WHERE
UserID = @.UserID
The ASPX page uses a user control named 'Address.ascx' which has 5
TextBoxes named txtAddress, txtCity, txtState, txtCountry & txtZip. I
am using the Get & Set statement for creating a property for each of
the TextBoxes like this:
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
'here comes the other properties
Finally, this is the ASPX page:
<%@. Register TagPrefix="NETConnect" TagName="Address"
src="http://pics.10026.com/?src=NETAddress.ascx" %>
<%@. Import Namespace="NConnect" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Sub Page_Load(....)
Dim boCart As Cart
Dim iUserID As Integer
Dim sqlReader As SqlDataReader
'retrieving the UserID by calling another function
sqlReader = boCart.GetAddress(iUserID)
End Sub
</script>
<form runat="server">
<NETConnect:NETAddress ID="ncBillingAddress" runat="server"/>
</form>
The ASPX page will render the 5 TextBoxes. Now how do I populate the 5
TextBoxes with the records that the stored procedure retrieves for a
particular UserID?
Populating a control with HTML file contents, images included?
publicvoid LoadToLabel()
{
using (StreamReader rdr =newStreamReader(FileUpload1.PostedFile.FileName))
{
string uploadedHTML = rdr.ReadToEnd();
textLabel.Text = uploadedHTML.ToString();
}
}
So far I have that, but the problem is that if the HTML file has an image displayed in it, the image doesn't show up because it's just copying the text. How would I be able to transfer the entire HTML page to a control? Like a panel or something like that, is it possible? And have the images come with it?
You should use LiteralControl for this purpose.
So if I switch it to a literal would it display the images as well?
I just switched it to a literal control and it still ignores the images. What must I do?
Yup. The LiteralControl will display the HTML exactly as you create it. See it as a place holder for output you create from code behind. Good luck!
OK, let me get this straight. You upload a HTML document containing <img> tags, which you then want to print out in the page, right?
If you do a view source in the resulting page, how does it look?
If the image urls in the uploaded page are relative then that is maybe what's causing the problem. Please give an example of how this tag looks like.
Also, if you upload a complete HTML document (with head/body) then you will render a page that is not valid (containing duplicate elements).
What does image urls being relative mean? Like pointing to a specific spot and not "~/Images/image.gif" etc.?
I did a view source on the page that uploads and displays the HTML document and yes you're right, there are now duplicate elements. It uploads everything within the uploaded documents <HTML></HTML> tags. How do I go about just grabbing what's in the body, and not the body tags themselves as that would be a duplicate element as well?
A relative path is one that does not begin with a / or http://something, example:
<img src="http://pics.10026.com/?src=images/ticker.gif">
A relative path is always calculated from the place where it is accessed.
If your (uploaded) page contains relative image paths, then to display these images correctly, you need to either rebuild the path (by parsing), or copy the contents of the path into the same folder as your upload page (not a viable solution).
Or you could make sure the user uploads all the images as well.
In either way, you have some parsing to do in order to get the uploaded file to display correctly. Also you must parse the file to get the actual contents of the file, and get past <html><body> and so on.
One option you have is to show the uploaded file in an iframe which would allow you to show the document as-is (although images would still require parsing).
How would I go about parsing the uploaded file so it displays properly with the images included within the literal control?
Strange... I could almost see that question coming Well, parsing text is not trivial, and I am not able to give you the code as it would take some time to develop and test. But here's how I would do: Define a regexp, that finds <img tags, and then replaces the src value with your "path prefixed" value.
Problem is that these tags can come in so many flavours, it's almost impossible to guarantee that the parser matches them correctly. Parsing HTML is kind of re-inventing the wheel again (the wheel being the browser ;-)
Uhm, I'm sorry, can't help you any further with this. But if you google a little on Regular expressions and spend some time trialing and erroring then you might reach your goal. Good luck!
Thank you so much for your help!
populating a drop-down problem
I then have a user log in to my site and their details are pulled off a database - including their address with state code.
I then set the selected value of the drop down to the users state.
The problem I have is if the sate on the database is empty or incorrect for some reason. When I set the selected value of the drop down list, I get an error because I'm trying to set it to a value that isn't there.
How do I get around this?
I need some sort of way to say - select this value from the dropdown only if the value exists - without giveing the error.
Any ideas?
ThanksmyComboBox.Items.Count will give you the number of items in the combo. If the number is not 0, you can use the SelectedValue, otherwise not.
That's not quite the problem but thanks anyway - what I mean is:
Lets say I have a drop down list with the values of
NY
CY
FL
NJ
and then I use dropdownList1.selectedValue = "DFGDF"
It's not going to find that value right?
What I wanted to know was is it possible to find out before I specify the selected value whether the value is actually in the list or not.
I have found a way round it though using:
Try
dropDownStates.SelectedValue = usrRow.Item("State")
Catch ex As Exception
End Try
At least that way I don't get an error but I was wondering if there's a way of telling what values are in the dropdown before specifying which is selected..
Thanks anyway.
Why not populate the ddl from the database and then you are sure that there is an entry for every possible selected item?
HTH
Well - the list of states is correct in that all the codes are correct and all the states are there. I need to provide all the states in the drop down to allow new users to sign up and for existing users to pick a new state if they move house.
The problem I have is that the data I'm initialy supllied with probably wont be perfect. I have no control over this data either - I just get the data and have to eork with it as it is. So I have to account for some of the users on the datanase not having the correct state code. Some might not have a code at all and others may be 'New York' rather then NY..
I suppose a way round it would be to:
create a dataset from the xml file of states
cerate a dataView from the dataset and see if the users state (NY for eaxmple) was in there.
if it was, I could then set the selected value of the dropdown to that value otherwise I would just leave it.
That just sounds like a long way of doing it though.
Isn't there some property of the dropdown like
dropdownlist.containsvalue ?
That would make it much easier.