Showing posts with label dynamic. Show all posts
Showing posts with label dynamic. Show all posts

Monday, March 26, 2012

Populate DataGrid from dynamic DropDown

Wow, it really sucks when you are trying to learn something and it just seems to not want to come together!

Anyways, my problem is this: I have a couple DropDownLists that are being populated from a database. Once the page loads, I am trying to set it up where the user can select an item from the DropDown, and then using a value associated the item in the DropDown, search the database again, and pull all matching records and place them in a DataGrid.

My code looks something like this:


<script runat="server"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
StatesDropDownList()
End IF
End Sub

Sub StatesDropDownList()

Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection (strConnection)
Dim strSqlStates As String = "SELECT DISTINCT State FROM Properties"

Dim objCommand As New OleDbCommand(strSqlStates, objConnection)
Dim objReader As OleDbDataReader = Nothing

objConnection.Open()
objReader = objCommand.ExecuteReader()

ddlStatesFilter.DataSource = objReader
ddlStatesFilter.DataTextField = "State"
ddlStatesFilter.DataValueField = "State"
ddlStatesFilter.DataBind()
objConnection.Close()
Dim DefaultState As ListItem = New ListItem
DefaultState.Text = "Select a State"
DefaultState.Value = "0"
ddlStatesFilter.Items.Insert(0, DefaultState)

End Sub

Sub DoSearch(Source as Object, E as EventArgs)

Dim strStates As String = ddlStatesFilter.SelectedItem.Value
Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection(strConnection)
Dim strSQL As String = "SELECT * FROM Properties WHERE State =" & ddlStatesFilter.SelectedItem.Value & " ORDER BY ID"

Dim objAdapter as New OleDbDataAdapter(strSQL, objConnection)

Dim objDataSet As New DataSet()
objAdapter.Fill(objDataSet, "dtProperties")

dgProperties.PagerStyle.NextPageText = "Next"
dgProperties.PagerStyle.PrevPageText = "Previous"

dgProperties.DataSource = objDataSet.Tables.Item("dtProperties")
dgProperties.DataBind()

End Sub
</script>

And then the control and the DataGrid look a little something like this:


<form runat="server>
<asp:literal ID="lbStatesFilter" runat="server" Text="State: " />
<asp:dropdownlist ID="ddlStatesFilter" runat="server" Font-Names="Verdana" Font-Size="8pt" />
<asp:button ID="SubmitSearch" runat="server" OnClick="DoSearch" Text="Search Listings" Font-Names="Verdana" Font-Size="8pt" Width="150" />
<asp:Datagrid id="dgProperties" runat="server"
AutoGenerateColumns="false"
Font-Size="8pt"
CellPadding="8"
AlternatingItemStyle-BackColor="#CCCCCC"
Font-Names="Verdana"
Width="100%"
AllowPaging="True"
OnPageIndexChanged="GridPageChange"
PageSize="20">
<HeaderStyle font-bold="True" forecolor="White" backcolor="#005BAC"></HeaderStyle>
<EditItemStyle backcolor="WhiteSmoke"></EditItemStyle>
<AlternatingItemStyle backcolor="Gainsboro"></AlternatingItemStyle>
<ItemStyle backcolor="WhiteSmoke"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" DataFormatString="{0:N0}" />
<asp:hyperlinkcolumn HeaderText="Resort Name" datanavigateurlfield="ID" datanavigateurlformatstring="details.aspx?id={0}" datatextfield="Resort_Name" />
<asp:BoundColumn DataField="City" HeaderText="City" />
<asp:BoundColumn DataField="State" HeaderText="State" />
<asp:BoundColumn DataField="Bedrooms" HeaderText="Bedrooms" />
<asp:BoundColumn DataField="Week" HeaderText="Week" />
<asp:BoundColumn DataField="Sale_Price" HeaderText="Sale Price" DataFormatString="{0:c2}" />
<asp:BoundColumn DataField="Rent_Price" HeaderText="Rent Price" DataFormatString="{0:c2}" />
</Columns>
</asp:Datagrid>
</form>

And here is the error that I am getting when I try and run it:


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.

Source Error:

Line 149:
Line 150: Dim objDataSet As New DataSet()
Line 151: objAdapter.Fill(objDataSet, "dtProperties")
Line 152:
Line 153:dgProperties.PagerStyle.NextPageText = "Next"

Source File: D:\...\search.aspx Line: 151

Thanks in advance for any help on this one. And sorry for so much code, but Im just not sure where the problem is coming from, so I want to make sure I give all the facts.

-JasonWithout looking at your code, basic concpets.
On page load, populate the drop down lists, enclose this in the If Page.IsPostBack = False, so it only happens once, obviously this can change based on your requirements.
Next, have a method that populates the datagrid, such as the dropdownlist selectedindexchange. Just use the selectedindex, value to hit the database to populate the datagrid.

Friday, March 16, 2012

Populating a text field and then getting its value

Hello All,
I am populating a text field dynamic in my code, and its a read-only
textbox control. It populates it perfectly fine. but when I get its value
back in code I get an empty string any clues as to how to get this sorted
please ?
Here is how I set the value of the control.
txtTitle.Text = feed.Channels[0].Title;
and get it later
String strString = txtTitle.Text;
Any clues please ?
Imran.Thats a strange one.
Working fine for me:
ASPX:
<asp:TextBox id="TextBox1" runat="server" ReadOnly="True"
OnClick="Button1_Click"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
CS
void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
TextBox1.Text = "Hello";
}
void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("Value: " + TextBox1.Text);
}
OUTPUT
Value: Hello
Try making a simple page like that and testing it
"Imran Aziz" <imran@.tb2.net> wrote in message
news:e43rNIcmFHA.2860@.TK2MSFTNGP15.phx.gbl...
> Hello All,
> I am populating a text field dynamic in my code, and its a read-only
> textbox control. It populates it perfectly fine. but when I get its value
> back in code I get an empty string any clues as to how to get this sorted
> please ?
> Here is how I set the value of the control.
> txtTitle.Text = feed.Channels[0].Title;
> and get it later
> String strString = txtTitle.Text;
> Any clues please ?
> Imran.
>
If I change the textbox property Readonly to false then it works perfectly
fine. I am using the latest Beta 2 release of .net framework.
Imran.
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:O0YmSUcmFHA.576@.TK2MSFTNGP15.phx.gbl...
> Thats a strange one.
> Working fine for me:
> ASPX:
> <asp:TextBox id="TextBox1" runat="server" ReadOnly="True"
> OnClick="Button1_Click"></asp:TextBox>
> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
> CS
> void Page_Load(object sender, System.EventArgs e)
> {
> if(!Page.IsPostBack)
> TextBox1.Text = "Hello";
> }
> void Button1_Click(object sender, System.EventArgs e)
> {
> Response.Write("Value: " + TextBox1.Text);
> }
> OUTPUT
> Value: Hello
>
> Try making a simple page like that and testing it
>
> "Imran Aziz" <imran@.tb2.net> wrote in message
> news:e43rNIcmFHA.2860@.TK2MSFTNGP15.phx.gbl...
>
used the exact code, but does not work for me, the text value returned is
empty.
Imran.
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:O0YmSUcmFHA.576@.TK2MSFTNGP15.phx.gbl...
> Thats a strange one.
> Working fine for me:
> ASPX:
> <asp:TextBox id="TextBox1" runat="server" ReadOnly="True"
> OnClick="Button1_Click"></asp:TextBox>
> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
> CS
> void Page_Load(object sender, System.EventArgs e)
> {
> if(!Page.IsPostBack)
> TextBox1.Text = "Hello";
> }
> void Button1_Click(object sender, System.EventArgs e)
> {
> Response.Write("Value: " + TextBox1.Text);
> }
> OUTPUT
> Value: Hello
>
> Try making a simple page like that and testing it
>
> "Imran Aziz" <imran@.tb2.net> wrote in message
> news:e43rNIcmFHA.2860@.TK2MSFTNGP15.phx.gbl...
>
okay, i'm on v1.1
I have beta 2 on a machine here.
If i have time to test it before i go, i'll try run the example there
"Imran Aziz" <imran@.tb2.net> wrote in message
news:%23rwRZUdmFHA.2860@.TK2MSFTNGP15.phx.gbl...
> If I change the textbox property Readonly to false then it works
> perfectly fine. I am using the latest Beta 2 release of .net framework.
> Imran.
> "Grant Merwitz" <grant@.workshare.com> wrote in message
> news:O0YmSUcmFHA.576@.TK2MSFTNGP15.phx.gbl...
>
Thanks a lot.
Imran.
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:u9$zKbdmFHA.3936@.TK2MSFTNGP10.phx.gbl...
> okay, i'm on v1.1
> I have beta 2 on a machine here.
> If i have time to test it before i go, i'll try run the example there
> "Imran Aziz" <imran@.tb2.net> wrote in message
> news:%23rwRZUdmFHA.2860@.TK2MSFTNGP15.phx.gbl...
>
Sorry it took me so long to try this.
But it's working fine for me on a Machine running Beta 2
"Imran Aziz" <imran@.tb2.net> wrote in message
news:ujgDxxdmFHA.2472@.TK2MSFTNGP15.phx.gbl...
> Thanks a lot.
> Imran.
> "Grant Merwitz" <grant@.workshare.com> wrote in message
> news:u9$zKbdmFHA.3936@.TK2MSFTNGP10.phx.gbl...
>
Hum strange I am doing something wrong then, I guess I will try and use
labels instead of text boxes.
Thanks a lot for coming back to me on that.
Imran
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:eUjjZmNnFHA.3572@.TK2MSFTNGP09.phx.gbl...
> Sorry it took me so long to try this.
> But it's working fine for me on a Machine running Beta 2
>
> "Imran Aziz" <imran@.tb2.net> wrote in message
> news:ujgDxxdmFHA.2472@.TK2MSFTNGP15.phx.gbl...
>