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 Dataset
Please tell me what I'm doing wrong.
check that if the dataset is populated when there is no PostBack.
Public Overloads Function GetBookInfo() As DataSet
Dim oPubConnection As SqlConnection
Dim SConnString As String
Dim osqlCommPubs As SqlCommand
Dim osqlCommTitles As SqlCommand
Dim oDataAdapterPubs As SqlDataAdapter
Dim oDataAdapterTitles As SqlDataAdapterTry
SConnString = "Data Source=(local);Initial Catalog=Pubs;" & _
"User ID=sa;Password=;"
oPubConnection = New SqlConnection(SConnString)
oPubConnection.Open()'Create command to retrieve pub info
osqlCommPubs = New SqlCommand
osqlCommPubs.Connection = oPubConnection
osqlCommPubs.CommandText = "Select Pub_ID, Pub_Name from Publishers"'Create Data Adapter for pub info
oDataAdapterPubs = New SqlDataAdapter
oDataAdapterPubs.SelectCommand = osqlCommPubs'Create command to retrieve title info
osqlCommTitles = New SqlCommand
osqlCommTitles.Connection = oPubConnection
osqlCommPubs.CommandText = "select Pub_ID, title, price, ytd_sales from titles"'Create data adapter for title info
oDataAdapterTitles = New SqlDataAdapter
oDataAdapterTitles.SelectCommand = osqlCommTitles'Create and fill a data set
Dim datBookInfo As DataSet = New DataSet
oDataAdapterPubs.Fill(datBookInfo, "Publishers")
oDataAdapterTitles.Fill(datBookInfo, "Titles")
Return datBookInfoCatch ex As Exception
Finally
oPubConnection.Close()End Try
End Function
If(!Page.IsPostBack)
{
LoadData();
}
Hmm, dont see anything obvious...How are you doing your bind of the dataset to the grid?
Also, since this is a windows project and not a web project (assuming your "I've created a vb windows application" is correct) there will NOT be a page.isPostBack...
Thanks,
MajorCats
Friday, March 16, 2012
Populating data in classes
I have 2 classes, for example. I have a Company class and a contact
class.
each class has the following properties
Company
ID
Name
Town
Ref
Contact
CompanyID
FirstName
LastName
Email
Now if i am retrieving the data from my database and filling the
contact class, is it ok to populate the contacts companyname, for
example
Contact.FirstName = "XXX"
Contact.lastname = "XXX"
Contact.Company.ID = 99
Contact.Company.Name = "XXXX"
Is this best practice, or should i be doing this another way? I
thought this would make sense to do, as most of the time if i have a
contact class, i usually want to show the companyName.
Anyone know of a better way to do this, or the "best practice way"??
CheersOr maybe it would be better to write another New constructor that
accepts the ID and Name?
Contact.firstname = "ZZZ"
Contact.Lastname = "CCC"
Contact.Company = New Company(99, "CCCCC")
Is this a better option?
Yes. This is a better option. Other than that, you look fine.
"Nemisis" <darrens2005@.hotmail.comwrote in message
news:1158670936.010052.6720@.d34g2000cwd.googlegrou ps.com...
Quote:
Originally Posted by
Or maybe it would be better to write another New constructor that
accepts the ID and Name?
>
Contact.firstname = "ZZZ"
Contact.Lastname = "CCC"
>
Contact.Company = New Company(99, "CCCCC")
>
Is this a better option?
>
tdavisjr wrote:
Quote:
Originally Posted by
Yes. This is a better option. Other than that, you look fine.
>
Quote:
Originally Posted by
Contact.firstname = "ZZZ"
Contact.Lastname = "CCC"
Contact.Company = New Company(99, "CCCCC")
Is this a better option?
Tdavis,
Thanks for the reply, i do agree with you, because it looks neater, but
do you have any other reason why you think this is better?
Populating ddl using hashtables
dropdownlist in a webform?The same way you bind any other source to a dropdownlist. Use "Key" and
"Value" as your dataTest/ValueField
ddl.DataSource = urHashtable;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();
--
MY ASP.Net tutorials
http://www.openmymind.net/
<shamila.thakur@.gmail.com> wrote in message
news:1129568158.495358.66850@.g14g2000cwa.googlegro ups.com...
>I have a hash table declared in a class. how do i bind it to a
> dropdownlist in a webform?
Populating ddl using hashtables
dropdownlist in a webform?The same way you bind any other source to a dropdownlist. Use "Key" and
"Value" as your dataTest/ValueField
ddl.DataSource = urHashtable;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();
MY ASP.Net tutorials
http://www.openmymind.net/
<shamila.thakur@.gmail.com> wrote in message
news:1129568158.495358.66850@.g14g2000cwa.googlegroups.com...
>I have a hash table declared in a class. how do i bind it to a
> dropdownlist in a webform?
>