Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Monday, March 26, 2012

populate detailview from code behind

i took it down to the basic level and still get an error -- Object reference not set to an instance of an object.

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

If DetailsView1.CurrentMode = DetailsViewMode.EditThen

CType(Me.DetailsView1.FindControl("textbox1"), TextBox).Text ="here"

EndIf

EndSub

<

asp:DetailsViewID="DetailsView1"

runat="server"

DefaultMode="Edit">

<Fields>

<asp:TemplateFieldHeaderText="Ticket Number">

<EditItemTemplate>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

</EditItemTemplate>

</asp:TemplateField>

</Fields>

</asp:DetailsView>

for all data control when you want to findcontrol in the row you should define which row you need to find the control therefore for the code you gave is not valid. it is becaus you are finding the control within the detailview list such as footer, header and other and not for the control in the row.

so may be you try thisCType(Me.DetailsView1.Rows.Item(index of the field).FindControl("textbox1"), TextBox).Text ="here"

I am not sure the code I given to you work or not but i am sure that it is the problem that i mention at above.

its the only thing in the detialview

CType(Me.DetailsView1.Rows.Item(0).FindControl("textbox1"), TextBox).Text = "here"

error i get -- Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


oh yeah i have found the solution already

DetailsView1.Rows.Item(DetailsView1.PageIndex).FindControl(

"textbox1")

for get to tell you the item index need to be the pageindex. giving you the wrong example


same error

built another project -- this works fine in the page load -- when i move it to my user control in my main project it screws up

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5


tpiazza:

same error

built another project -- this works fine in the page load -- when i move it to my user control in my main project it screws up

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5

did you change the item index from 0 to details.pageindex?

CType(Me.Details1.Rows.Item(0).FindControl("drop1"), DropDownList).SelectedValue = 5

to

CType(Me.Details1.Rows.Item(Details1.PageIndex).FindControl("drop1"), DropDownList).SelectedValue = 5


Hello,

Look at these samples, especially "DetailsView Editing". They provide much codes and sample to follow.

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

HTH

Saturday, March 24, 2012

populate dropdownlist in datagrid

Really struggline with how to populate a dropdownlist in a datagrid when i click the edit link:

Sub dgrdResults_PageIndexChanged (s as Object, e as DataGridPageChangedEventArgs)
dgrdResults.CurrentPageIndex = e.NewPageIndex
BindDataGrid
End Sub

Sub dgrdResults_CancelCommand(s as Object, e as DataGridCommandEventArgs)
dgrdResults.EditItemIndex = -1
BindDataGrid
End Sub

Sub dgrdResults_UpdateCommand(s as Object, e as DataGridCommandEventArgs)
Dim TempList as DropDownList
Dim TempValue as String

TempList = e.Item.FindControl("drpUnitRating")
TempValue = TempList.SelectedItem.Value

dgrdResults.EditItemIndex = -1
BindDataGrid
End Sub

Function BindRating()
'Dim colArrayList as New ArrayList
'colArrayList.Add("5")
'colArrayList.Add("4")
'colArrayList.Add("3")
'colArrayList.Add("2")
'colArrayList.Add("1")
'drpUnitRating.DataSource = colArrayList
'drpUnitRating.DataBind()
End Function

Sub dgrdResults_EditCommand(s as Object, e as DataGridCommandEventArgs)
dgrdResults.EditItemIndex = e.Item.ItemIndex
BindDataGrid
End Sub

.....

<asp:DataGrid
ID="dgrdResults"
AllowPaging="true"
AutoGenerateColumns="false"
PagerStyle-Mode="NumericPages" PagerStyle-PageButtonCount="5" PagerStyle-Position="Bottom"
PageSize="25"
OnPageIndexChanged="dgrdResults_PageIndexChanged"
OnEditCommand="dgrdResults_EditCommand"
OnUpdateCommand="dgrdResults_UpdateCommand"
OnCancelCommand="dgrdResults_CancelCommand"
CellPadding="0"
BorderWidth="0"
GridLines="None" CssClass="../fmcss.css"
runat="server" >
<columns>
<asp:TemplateColumn>
<itemTemplate>
<%# Container.DataItem("PRD_BRAND") %> <%# Container.DataItem("PRD_MODEL") %> <%# Container.DataItem("PRD_NAME") %> <%# Container.DataItem("PRD_CATEGORY_CD") %>
</itemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<itemTemplate>
<%# Container.DataItem("UNIT_RATING") %>
</itemTemplate>
<edititemtemplate>
<asp:DropDownList ID="drpUnitRating" DataTextField='<%# Container.DataItem("UNIT_RATING") %>' DataValueField='<%# Container.DataItem("UNIT_RATING") %>' DataSource='<%# BindRating %>' runat="server"></asp:DropDownList>
</edititemtemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="E" UpdateText="U" CancelText="C" />
</columns>
</asp:DataGrid>

I can do it either by connecting to a database or by just typing in say 5 values 1-5. I will be doing it both ways once I figure out how to do this. Right now I am just trying to just do it without the connecting to a database.

Thanks in advance to any replies.try this link:

http://aspnet.4guysfromrolla.com/articles/051904-1.aspx

hth,
Kev
Kevin,

Nice link.

Anyone know how to translate this to vb.net?

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
// see if we are working with the row being edited
if (e.Item.ItemType == ListItemType.EditItem)
{
// populate the division DDL
DropDownList ddlDivision = (DropDownList) e.Item.FindControl("ddlDivision");

// connect to Access DB
OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["connString"]);
myConnection.Open();

const string SQL = @."SELECT DivisionID, Name FROM Divisions ORDER BY Name";
OleDbCommand myCommand = new OleDbCommand(SQL, myConnection);

ddlDivision.DataSource = myCommand.ExecuteReader();
ddlDivision.DataBind();

myConnection.Close();

// now, select the appropriate division
int currentDivisionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "DivisionID"));
ListItem li = ddlDivision.Items.FindByValue(currentDivisionID.ToString());
if (li != null) li.Selected = true;

// finally, populate the department DDL based on the selected division
int currentDepartmentID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "DepartmentID"));
if (li != null) li.Selected = true;

DropDownList ddlDepartment = (DropDownList) e.Item.FindControl("ddlDepartment");

PopulateDepartmentBasedOnDivision(ddlDepartment, ddlDivision, currentDepartmentID);
}
}

Wednesday, March 21, 2012

Populating a Drop-Down List

I was wondering if anyone knows a relatively simple way to populate a drop-down list.

I cannot use the object data bindings, I am currently required to do it in the code, using the databind() commands.

I would like to populate this drop-down with the use of a Stored Proc....

I am using Visual Studio 2005, SQL Server 2005, and ASP.NET 2.0

If anyone could give me some tips, that would be great!

You should find this simple and helpful:

http://msconline.maconstate.edu/tutorials/ASPNET20/ASPNET06/aspnet06-06.aspx

Populating a dropdown box

Hi all,

I have populated one dropdown menu using data from a database using the following code

Sub Page_Load (seander as Object, e as EventArgs) Year.DataSource = GetYear() Year.DataBind()End Sub Function GetYear() As System.Data.SqlClient.SqlDataReader Dim connectionString As String = "server='localhost'; user id='sa'; password='eoyson'; Database='PupilRoll'" Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString) Dim queryString As String = "SELECT [Years].* FROM [Years]" Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection) sqlConnection.Open Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection) Return dataReader End Function
I am now trying to populate another dropdown list depending on the information selected from the previous dropdown list.
Cheers!

Try the tutorial right on this site:

Master Detail Filtering with Two Dropdownlists

populating a dropdown list from arraylist of objects

hi there,
i have an arraylist which holds my Customer object, that contains the
customer details, but when i try binding it to a dropdown box, i cant get
the values to appear..
this is what i tried:
customerList.DataSource = Customer.GetAdminList()
customerList.DataBind()
customerList.DataTextField = CType(customerList.DataSource,
Customer).Email.ToString
customerList.DataValueField = CType(customerList.DataSource,
Customer).CustomerID.ToString
customerList.Items.Insert(0, "--ALL CUSTOMERS--")
this error returns me a invalid cast error..so i also tried the normal
method:
customerList.DataSource = DotcoCustomer.GetAdminList()
customerList.DataBind()
customerList.DataTextField = "email"
customerList.DataValueField = "customerID"
customerList.Items.Insert(0, "--ALL CUSTOMERS--")
but this populates the dropdown list with the the values
"Web2004.Customer" - which is my application namespace.Object...'
any help appreciated,
Paul.Rearrange the order of your code:
customerList.DataSource = DotcoCustomer.GetAdminList()
customerList.DataTextField = "email"
customerList.DataValueField = "customerID"
customerList.DataBind()
customerList.Items.Insert(0, "--ALL CUSTOMERS--")
"Paul M" <milsnips@.hotmail.com> wrote in message
news:uf9aWLvQEHA.3568@.TK2MSFTNGP10.phx.gbl...
> hi there,
> i have an arraylist which holds my Customer object, that contains the
> customer details, but when i try binding it to a dropdown box, i cant get
> the values to appear..
> this is what i tried:
> customerList.DataSource = Customer.GetAdminList()
> customerList.DataBind()
> customerList.DataTextField = CType(customerList.DataSource,
> Customer).Email.ToString
> customerList.DataValueField = CType(customerList.DataSource,
> Customer).CustomerID.ToString
> customerList.Items.Insert(0, "--ALL CUSTOMERS--")
> this error returns me a invalid cast error..so i also tried the normal
> method:
>
> customerList.DataSource = DotcoCustomer.GetAdminList()
> customerList.DataBind()
> customerList.DataTextField = "email"
> customerList.DataValueField = "customerID"
> customerList.Items.Insert(0, "--ALL CUSTOMERS--")
>
> but this populates the dropdown list with the the values
> "Web2004.Customer" - which is my application namespace.Object...'
> any help appreciated,
> Paul.
>
>

populating a dropdown list from arraylist of objects

hi there,

i have an arraylist which holds my Customer object, that contains the
customer details, but when i try binding it to a dropdown box, i cant get
the values to appear..

this is what i tried:

customerList.DataSource = Customer.GetAdminList()
customerList.DataBind()
customerList.DataTextField = CType(customerList.DataSource,
Customer).Email.ToString
customerList.DataValueField = CType(customerList.DataSource,
Customer).CustomerID.ToString
customerList.Items.Insert(0, "--ALL CUSTOMERS--")

this error returns me a invalid cast error..so i also tried the normal
method:

customerList.DataSource = DotcoCustomer.GetAdminList()
customerList.DataBind()
customerList.DataTextField = "email"
customerList.DataValueField = "customerID"
customerList.Items.Insert(0, "--ALL CUSTOMERS--")

but this populates the dropdown list with the the values
"Web2004.Customer" - which is my application namespace.Object...??
any help appreciated,

Paul.Rearrange the order of your code:
customerList.DataSource = DotcoCustomer.GetAdminList()
customerList.DataTextField = "email"
customerList.DataValueField = "customerID"
customerList.DataBind()
customerList.Items.Insert(0, "--ALL CUSTOMERS--")

"Paul M" <milsnips@.hotmail.com> wrote in message
news:uf9aWLvQEHA.3568@.TK2MSFTNGP10.phx.gbl...
> hi there,
> i have an arraylist which holds my Customer object, that contains the
> customer details, but when i try binding it to a dropdown box, i cant get
> the values to appear..
> this is what i tried:
> customerList.DataSource = Customer.GetAdminList()
> customerList.DataBind()
> customerList.DataTextField = CType(customerList.DataSource,
> Customer).Email.ToString
> customerList.DataValueField = CType(customerList.DataSource,
> Customer).CustomerID.ToString
> customerList.Items.Insert(0, "--ALL CUSTOMERS--")
> this error returns me a invalid cast error..so i also tried the normal
> method:
>
> customerList.DataSource = DotcoCustomer.GetAdminList()
> customerList.DataBind()
> customerList.DataTextField = "email"
> customerList.DataValueField = "customerID"
> customerList.Items.Insert(0, "--ALL CUSTOMERS--")
>
> but this populates the dropdown list with the the values
> "Web2004.Customer" - which is my application namespace.Object...??
> any help appreciated,
> Paul.

Friday, March 16, 2012

Populating a Stream object with XML and then convert Stream into a String Options

I am trying to populate a Stream via an XML Serializer and then
convert that stream of xml into a string.

I have been able to successfully serialize an object into a physical
xml file written to my local disk. The XML file has content and looks
as I would expect, here is the code for that action:

TextWriter writer = new StreamWriter(@dotnet.itags.org."c:\test.xml");
serializer.Serialize(writer, oData);

When I try to to serialize my object to a stream of xml and then
convert to a string I get no content in the string. Both the
MemoryStream and StreamWriter object appear to have the correct byte
size after I populate them with the serialized data. Is there an
alternative way I should be doing this? Here is the code I'm using:

MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);

serializer.Serialize(sw, oData);

StreamReader sr = new StreamReader(ms);

string test = "";
test = sr.ReadToEnd(); //No content is populated, why?

Thank you,
Darren

dstahl:

MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);

serializer.Serialize(sw, oData);

StreamReader sr = new StreamReader(ms);

string test = "";
test = sr.ReadToEnd(); //No content is populated, why?

Thank you,
Darren

Hi Darren,

In your code, after you serialize your sw, you couldn't new it again, because that will create sw again.

Try to cancel that line : "StreamReader sr = new StreamReader(ms); ". As you know, if you do like your way, ms has nothing, then sw has nothing too, even if you have initialize the sw before.

Hope this helps. Thanks.