Showing posts with label created. Show all posts
Showing posts with label created. Show all posts

Wednesday, March 21, 2012

Populating a datagrid ... how is it done normally?

Hi All ...
I'm writing a small shopping cart app (school assignment) and need to list
all the books that a user selects.
I have created a "BookBasket" object that contains among other things an
arraylist of the user selected books. The "book" object contains isbn,title
price and quantity information.
I'd like to display the contents of the BookBasket in a datagrid control.
I assume that to start with I'd have to iterate through the basket to
identify each books information, but then how do I populate the datagrid
from there?
It seems from what I've read,that it is easier to populate a datagrid from a
datasource, but I don't know how that would relate to what I'm doing.
any clues or links to examples would be much appreciated.
Thanks in advance
Prime.Prime,
There is a plethora of examples on using a datagrid control. Search
google and you will find more than you need. The second article outlines
what kind of datasources a datagrid will accept, an array is a valid data
source so you won't have to write any code to fill another control. The
third snippit supports my last statement.
Hope this helps.
Jared
Here's one to get you started
http://samples.gotdotnet.com/quicks...c_datagrid.aspx
http://msdn.microsoft.com/library/d...nadodataset.asp
You can bind to the following structures if their elements expose public
properties:
a.. Any component that implements the IList interface. This includes
single-dimension arrays
"Prime" <not@.real.address> wrote in message
news:uIhFN$0pEHA.1668@.TK2MSFTNGP14.phx.gbl...
> Hi All ...
> I'm writing a small shopping cart app (school assignment) and need to list
> all the books that a user selects.
> I have created a "BookBasket" object that contains among other things an
> arraylist of the user selected books. The "book" object contains
> isbn,title
> price and quantity information.
> I'd like to display the contents of the BookBasket in a datagrid control.
> I assume that to start with I'd have to iterate through the basket to
> identify each books information, but then how do I populate the datagrid
> from there?
> It seems from what I've read,that it is easier to populate a datagrid from
> a
> datasource, but I don't know how that would relate to what I'm doing.
> any clues or links to examples would be much appreciated.
> Thanks in advance
> Prime.
>
>
Hi Jared,
I think you've helped me before ... thank-you again.
I found the gotdotnet sample very helpful. We are currently using a text
book that is poor by any standard so your assistance is very much
appreciated.
The second sample was good, but we're not allowed to use a database
interface! I originally coded my app that way,but was told that it was
unacceptable. :( I found the database version extremely simple to code.)
Again, thanks for you assistance.
Prime.
"Jared" <ask_me_for_it@.nospam.com> wrote in message
news:10lpaldc1uahra6@.corp.supernews.com...
> Prime,
> There is a plethora of examples on using a datagrid control. Search
> google and you will find more than you need. The second article outlines
> what kind of datasources a datagrid will accept, an array is a valid data
> source so you won't have to write any code to fill another control. The
> third snippit supports my last statement.
> Hope this helps.
> Jared
> Here's one to get you started
>
http://samples.gotdotnet.com/quicks... />
agrid.aspx
>
http://msdn.microsoft.com/library/d...nadodataset.asp
> You can bind to the following structures if their elements expose public
> properties:
> a.. Any component that implements the IList interface. This includes
> single-dimension arrays
> "Prime" <not@.real.address> wrote in message
> news:uIhFN$0pEHA.1668@.TK2MSFTNGP14.phx.gbl...
list
control.
from
>

Populating a datagrid ... how is it done normally?

Hi All ...

I'm writing a small shopping cart app (school assignment) and need to list
all the books that a user selects.

I have created a "BookBasket" object that contains among other things an
arraylist of the user selected books. The "book" object contains isbn,title
price and quantity information.

I'd like to display the contents of the BookBasket in a datagrid control.

I assume that to start with I'd have to iterate through the basket to
identify each books information, but then how do I populate the datagrid
from there?

It seems from what I've read,that it is easier to populate a datagrid from a
datasource, but I don't know how that would relate to what I'm doing.

any clues or links to examples would be much appreciated.

Thanks in advance

Prime.Prime,
There is a plethora of examples on using a datagrid control. Search
google and you will find more than you need. The second article outlines
what kind of datasources a datagrid will accept, an array is a valid data
source so you won't have to write any code to fill another control. The
third snippit supports my last statement.
Hope this helps.
Jared

Here's one to get you started
http://samples.gotdotnet.com/quicks...c_datagrid.aspx

http://msdn.microsoft.com/library/d...nadodataset.asp

You can bind to the following structures if their elements expose public
properties:

a.. Any component that implements the IList interface. This includes
single-dimension arrays

"Prime" <not@.real.address> wrote in message
news:uIhFN$0pEHA.1668@.TK2MSFTNGP14.phx.gbl...
> Hi All ...
> I'm writing a small shopping cart app (school assignment) and need to list
> all the books that a user selects.
> I have created a "BookBasket" object that contains among other things an
> arraylist of the user selected books. The "book" object contains
> isbn,title
> price and quantity information.
> I'd like to display the contents of the BookBasket in a datagrid control.
> I assume that to start with I'd have to iterate through the basket to
> identify each books information, but then how do I populate the datagrid
> from there?
> It seems from what I've read,that it is easier to populate a datagrid from
> a
> datasource, but I don't know how that would relate to what I'm doing.
> any clues or links to examples would be much appreciated.
> Thanks in advance
> Prime.
>
Hi Jared,

I think you've helped me before ... thank-you again.

I found the gotdotnet sample very helpful. We are currently using a text
book that is poor by any standard so your assistance is very much
appreciated.

The second sample was good, but we're not allowed to use a database
interface! I originally coded my app that way,but was told that it was
unacceptable. :( I found the database version extremely simple to code.)

Again, thanks for you assistance.

Prime.

"Jared" <ask_me_for_it@.nospam.com> wrote in message
news:10lpaldc1uahra6@.corp.supernews.com...
> Prime,
> There is a plethora of examples on using a datagrid control. Search
> google and you will find more than you need. The second article outlines
> what kind of datasources a datagrid will accept, an array is a valid data
> source so you won't have to write any code to fill another control. The
> third snippit supports my last statement.
> Hope this helps.
> Jared
> Here's one to get you started
http://samples.gotdotnet.com/quicks...c_datagrid.aspx
>
http://msdn.microsoft.com/library/d...nadodataset.asp
> You can bind to the following structures if their elements expose public
> properties:
> a.. Any component that implements the IList interface. This includes
> single-dimension arrays
> "Prime" <not@.real.address> wrote in message
> news:uIhFN$0pEHA.1668@.TK2MSFTNGP14.phx.gbl...
> > Hi All ...
> > I'm writing a small shopping cart app (school assignment) and need to
list
> > all the books that a user selects.
> > I have created a "BookBasket" object that contains among other things an
> > arraylist of the user selected books. The "book" object contains
> > isbn,title
> > price and quantity information.
> > I'd like to display the contents of the BookBasket in a datagrid
control.
> > I assume that to start with I'd have to iterate through the basket to
> > identify each books information, but then how do I populate the datagrid
> > from there?
> > It seems from what I've read,that it is easier to populate a datagrid
from
> > a
> > datasource, but I don't know how that would relate to what I'm doing.
> > any clues or links to examples would be much appreciated.
> > Thanks in advance
> > Prime.

Populating a DataGrid with a Stored Procedure based on ddl values

Hello All -

First off, I am a complete newbie.

I have created a page with 5 dropdownlists that I populated using stored procedures.

Question one:

How Do I display a default value for these Dropdowns?

I have configured a DataGrid using another Stored Procedure.

Question 2:

How Do I populate the DG with the values specified within the 5 dropdownlists using a stored procedure?

Here is a copy of the current aspx page.

<%@dotnet.itags.org.PageLanguage="VB"MasterPageFile="~/MasterPage.master"Title="CTM PreRunbook" %>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<table><tr><thalign="center"colspan="3"style="vertical-align: middle; text-align: center"valign="middle">

<asp:LabelID="Label1"runat="server"Font-Size="Large"Text="Data Feed Query"></asp:Label> </th></tr>

<tr>

<th>

Application Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAppName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSource1"DataTextField="AppName"DataValueField="AppName">

<asp:ListItem>- Select -</asp:ListItem>

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_AppName"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Data Feed Name:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlDataFeedName"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceDependantFeeds"DataTextField="FeedName"DataValueField="FeedName">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceDependantFeeds"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_FeedNames"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Account:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlAccount"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceServiceAccount"DataTextField="ServiceAccounts"DataValueField="ServiceAccounts">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceServiceAccount"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGetDistinctServiceAccounts"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Platform:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px; text-align: left;">

<asp:DropDownListID="ddlPlatform"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourcePlatform"DataTextField="Platform"DataValueField="Platform">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourcePlatform"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spGet_Platform"SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

<tr>

<th>

Scheduled Run Days:</th>

<tdalign="right"style="vertical-align: top; text-align: left">

<tdstyle="width: 31px">

<asp:DropDownListID="ddlSchedRunDays"runat="server"AutoPostBack="True"DataSourceID="SqlDataSourceSchedRunDays"DataTextField="ScheduledRunDays"DataValueField="ScheduledRunDays">

</asp:DropDownList><asp:SqlDataSourceID="SqlDataSourceSchedRunDays"runat="server"

ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"SelectCommand="spGet_ScheduledRunDays"

SelectCommandType="StoredProcedure"></asp:SqlDataSource>

</td></tr>

</Table>

<table>

<tr>

<tdstyle="width: 100px"align="center">

<asp:ButtonID="btnFilter"runat="server"Text="Filter"/></td>

<tdstyle="width: 100px">

<asp:ButtonID="btnReset"runat="server"Text="Reset"/></td>

</tr>

<tr>

<tdcolspan="2"align="center">

<asp:ButtonID="btnShowAllDataFeeds"runat="server"Text="Show All Data Feeds"/></td>

</tr>

</table>

<br/>

<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"DataSourceID="SqlDataSourceGridView">

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSourceGridView"runat="server"ConnectionString="<%$ ConnectionStrings:PreRunbookConnectionString %>"

SelectCommand="spFilterByServAcct"SelectCommandType="StoredProcedure">

<SelectParameters>

<asp:FormParameterFormField="ServiceAccounts"Name="ServAcct"

Type="String"/>

</SelectParameters>

</asp:SqlDataSource>

<br>

</asp:Content>

Thanks ,

Pat

here is the best example that describes your needs

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/sqldatasource.aspx

Populating a Dataset

I've created a vb windows application and written the following function within a class (class1). I dragged a dataset onto my form and renamed it datBookInfo but when I run the code I don't get anything in the form.

Please tell me what I'm doing wrong.


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 SqlDataAdapter

Try
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 datBookInfo

Catch ex As Exception
Finally
oPubConnection.Close()

End Try

End Function

check that if the dataset is populated when there is no PostBack.


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 a List Box

Hi

I am trying to populate a list box with all my themes that i have created but I keep getting an message which is

Type DirectoryInfo is not defined i m guessing i have to declare if but i dont know how are where please could you guys help. Thanks

1Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)23If (Not Page.IsPostBack)Then45 Dim themeDirAs New DirectoryInfo(Server.MapPath("App_Themes"))6 lstThemes.DataTextField ="Name"7 lstThemes.DataSource = themeDir.GetDirectories()8 lstThemes.DataBind()910End If1112 End Sub13

The DirectoryInfo class is located under the System.IO namespace.


Yeah i tired declaring it as Inherits System.IO. Is that the correct appraoch


hello i ve tried to use the system io like u suggested but i still have the same problem here s my code. Do u have any ideas where i am going wrong cheers

PartialClass _Default

Inherits System.Web.UI.Page

Inherits System.IO

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

If (Not Page.IsPostBack)Then

Dim themeDirAsNew DirectoryInfo(Server.MapPath("App_Themes"))

lstThemes.DataTextField ="Name"

lstThemes.DataSource = themeDir.GetDirectories()

lstThemes.DataBind()

EndIf

EndSub

EndClass


Is your App_Thems directory in your web directory? I would check the path returned by Server.MapPath("App_Theme"), perhaps it doesn't exists and that's why you're getting an error.

bigyawn_hippo:

hello i ve tried to use the system io like u suggested but i still have the same problem here s my code. Do u have any ideas where i am going wrong cheers

PartialClass _DefaultInherits System.Web.UI.Page

Inherits System.IO

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

If (Not Page.IsPostBack)Then

Dim themeDirAsNew DirectoryInfo(Server.MapPath("App_Themes"))

lstThemes.DataTextField ="Name"

lstThemes.DataSource = themeDir.GetDirectories()

lstThemes.DataBind()

EndIf

EndSub

EndClass

Hi,

Based on my understanding, you want to import the System.IO namespace in your asp.net VB application. If I have misunderstood you, please feel free to let me know.

The DirectoryInfo class is in the System.IO. If we want to use it, we can import it. For example:

Imports System.IOPartialClass _DefaultInherits System.Web.UI.PageProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)If (Not Page.IsPostBack)Then Dim themeDirAs New DirectoryInfo(Server.MapPath("App_Themes")) lstThemes.DataTextField ="Name" lstThemes.DataSource = themeDir.GetDirectories() lstThemes.DataBind()End If End SubEnd Class

Alternatively, we can use this namespace in the code behind directly, for example:

Dim themeDir As NewSystem.IO.DirectoryInfo(Server.MapPath("App_Themes"))

Besides, you can find more information about asp.net by the following link:http://www.asp.net/get-started/.


I hope this helps.


Hi hyppo,

why don't you try to put only this two instructions ?

lstThemes.DataSource = themeDir.GetDirectories()
lstThemes.DataBind()

I found my code doing the same and i didn' t write ...DataTextField

Populating a list box from a loop

I was wondering how you would populate a listbox in asp.net with values from an array created by a loop.
here is the loop structure I am using:
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string

MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)

For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next
lblReasonforError.Text=Msg

End Sub

I can get it to populate the first value in the array but I need it to populate all the values in the array which will vary according to the record it is looking at. Then with those value(s) I want to populate a listbox with all the error codes that are stored in a table on sql server according to the results of the split.
For example:
tblErrorCodes
Code ErrorMessage
1 Wrong
2 Bad

Split gets the value of 1 and 2
Populate listbox like so:
Wrong
Bad
Hope this is clear
Thank you

You can simply bind datalist to an array by setting the array as the datasource of the datalist
http://aspnet.4guysfromrolla.com/articles/082504-1.aspx
Hope that helps


uncleb wrote:

You can simply bind datalist to an array by setting the array as the datasource of the datalist


It's the simplest and the only way since ListBox doesn't have Add method.
Madmaxrtw,I'm not sure what your code is about.
But in case you need to use just some items you'd have to transform array into another one.
Or might be use DataTextField and DataValueField properties.

Hey guys I got it here is the loop script

<script language="VB" runat="server">
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string


MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)


For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next

dlErrorList.DataSource=MyArray
dlErrorList.DataBind()

End Sub
</script>

And the DataList:
<asp:datalist CellPadding="10" ID="dlErrorList" RepeatColumns="0"
RepeatDirection="Vertical" runat="server" gridline="Both">
<ItemTemplate>
<b><%# Container.DataItem %></b>
</ItemTemplate>
</asp:datalist>

Thank you so MUCH!!!


Yakov72 wrote:

It's the simplest and the only way since ListBox doesn't have Add method.



Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.

BobT wrote:


Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.


You're right.
Thank you for corrections.