By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,957 Members | 1,218 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,957 IT Pros & Developers. It's quick & easy.

Adding dummy item to DropDownList in a DataGrid

P: n/a
Hi folks,

I have a DropDownList in a DataGrid that is populated from records in a
database.

I want to add a value that might be a string such as "Select a Company" for
the first item since an OnSelectedIndex event is not fired if you select the
first item.

Does anyone know of an easy way to do this?

I am using the following code to get at the the dropdownlist that is inside
a cell within the datagrid which is placed inside my PageLoad function:

Dim list As DropDownList = New DropDownList ( )
Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)

Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)

ddlCo .Items.Add("Select a Company")

I am getting the infamous "object reference not set to an instance of an
object" on the Dim item line...

Appreciate any replies...
glenn


Apr 25 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent control
of the cell control cast as a TableCell object. But at this point the list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:
Hi folks,

I have a DropDownList in a DataGrid that is populated from records in a
database.

I want to add a value that might be a string such as "Select a Company" for
the first item since an OnSelectedIndex event is not fired if you select the
first item.

Does anyone know of an easy way to do this?

I am using the following code to get at the the dropdownlist that is inside
a cell within the datagrid which is placed inside my PageLoad function:

Dim list As DropDownList = New DropDownList ( )
Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)

Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)

ddlCo .Items.Add("Select a Company")

I am getting the infamous "object reference not set to an instance of an
object" on the Dim item line...

Appreciate any replies...
glenn

Apr 25 '06 #2

P: n/a
I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn

"clickon" wrote:
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent control
of the cell control cast as a TableCell object. But at this point the list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:
Hi folks,

I have a DropDownList in a DataGrid that is populated from records in a
database.

I want to add a value that might be a string such as "Select a Company" for
the first item since an OnSelectedIndex event is not fired if you select the
first item.

Does anyone know of an easy way to do this?

I am using the following code to get at the the dropdownlist that is inside
a cell within the datagrid which is placed inside my PageLoad function:

Dim list As DropDownList = New DropDownList ( )
Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)

Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)

ddlCo .Items.Add("Select a Company")

I am getting the infamous "object reference not set to an instance of an
object" on the Dim item line...

Appreciate any replies...
glenn

Apr 25 '06 #3

P: n/a
Just add it to your SQL

SELECT "add a company"
UNION
SELECT CompanyName from Companies

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I
could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn

"clickon" wrote:
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent
control
of the cell control cast as a TableCell object. But at this point the
list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:
> Hi folks,
>
> I have a DropDownList in a DataGrid that is populated from records in a
> database.
>
> I want to add a value that might be a string such as "Select a Company"
> for
> the first item since an OnSelectedIndex event is not fired if you
> select the
> first item.
>
> Does anyone know of an easy way to do this?
>
> I am using the following code to get at the the dropdownlist that is
> inside
> a cell within the datagrid which is placed inside my PageLoad function:
>
> Dim list As DropDownList = New DropDownList ( )
> Dim cell As TableCell = CType(list.Parent, TableCell)
> Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>
> Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
> DropDownList)
>
> ddlCo .Items.Add("Select a Company")
>
> I am getting the infamous "object reference not set to an instance of
> an
> object" on the Dim item line...
>
> Appreciate any replies...
> glenn
>
>
>
>

Apr 25 '06 #4

P: n/a
This line
Dim list As DropDownList = New DropDownList ( )

creates a new DdropDownList control called list, it is not in a DataGrid, it
has no parent or child control, it is not on the page at all, it is
esentially just an object. Therefore list.parent makes no sense unless there
is a line such as

somecontrol.Controls.Add(list);

Which will place the ddl inside some other control.
If you wanht to add a dummy item to a DropDownList, that is declared on your
page you can do it as below.

<asp:DropDownList runat="server" ID="MyDDL" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Because AppendDataBoundItems is set to true then the items from the database
will be added after the dummy item.

"glenn" wrote:
I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn

"clickon" wrote:
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent control
of the cell control cast as a TableCell object. But at this point the list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:
Hi folks,

I have a DropDownList in a DataGrid that is populated from records in a
database.

I want to add a value that might be a string such as "Select a Company" for
the first item since an OnSelectedIndex event is not fired if you select the
first item.

Does anyone know of an easy way to do this?

I am using the following code to get at the the dropdownlist that is inside
a cell within the datagrid which is placed inside my PageLoad function:

Dim list As DropDownList = New DropDownList ( )
Dim cell As TableCell = CType(list.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)

Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)

ddlCo .Items.Add("Select a Company")

I am getting the infamous "object reference not set to an instance of an
object" on the Dim item line...

Appreciate any replies...
glenn

Apr 25 '06 #5

P: n/a
When used without a datasource in a ddl, your example works.

However, when used with a datasource in a ddl, your example does not add the
dummy item.

My code is as follows:

<asp:DropDownList id="ddlTo"
AppendDataBoundItems="true" runat="server" DataValueField="cname"
AutoPostBack="True" DataSource='<%# GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged ">
<asp:ListItem Selected="True"
Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Thanks again for any tips.
Glenn

"clickon" wrote:
This line
Dim list As DropDownList = New DropDownList ( )

creates a new DdropDownList control called list, it is not in a DataGrid, it
has no parent or child control, it is not on the page at all, it is
esentially just an object. Therefore list.parent makes no sense unless there
is a line such as

somecontrol.Controls.Add(list);

Which will place the ddl inside some other control.
If you wanht to add a dummy item to a DropDownList, that is declared on your
page you can do it as below.

<asp:DropDownList runat="server" ID="MyDDL" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Because AppendDataBoundItems is set to true then the items from the database
will be added after the dummy item.

"glenn" wrote:
I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn

"clickon" wrote:
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent control
of the cell control cast as a TableCell object. But at this point the list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:

> Hi folks,
>
> I have a DropDownList in a DataGrid that is populated from records in a
> database.
>
> I want to add a value that might be a string such as "Select a Company" for
> the first item since an OnSelectedIndex event is not fired if you select the
> first item.
>
> Does anyone know of an easy way to do this?
>
> I am using the following code to get at the the dropdownlist that is inside
> a cell within the datagrid which is placed inside my PageLoad function:
>
> Dim list As DropDownList = New DropDownList ( )
> Dim cell As TableCell = CType(list.Parent, TableCell)
> Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>
> Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"), DropDownList)
>
> ddlCo .Items.Add("Select a Company")
>
> I am getting the infamous "object reference not set to an instance of an
> object" on the Dim item line...
>
> Appreciate any replies...
> glenn
>
>
>
>

Apr 25 '06 #6

P: n/a
The UNION statement only works as shown:

SELECT statement
UNION
SELECT statement

where a statement would be SELECT * FROM TABLE

"Jeff Dillon" wrote:
Just add it to your SQL

SELECT "add a company"
UNION
SELECT CompanyName from Companies

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
I am looking to add a dummy value to my ddl so that when I select the first
real value in the ddl, an OnSelectedIndex event will be fired. The value
might be for example, "Select a Company".

The code I presented I believe needs to be provided in order to find a
control that is inside a DataGrid. If the ddl was not in a DataGrid I
could
simply write something like:
Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
but since it is inside the DataGrid, I needed the additonal code.

HTH. If you know of an easier way, please let me know.
Thanks,
glenn

"clickon" wrote:
You code makes no sense for example

Dim list As DropDownList = New DropDownList ( )

declares and initiates a DropdownList control called list, then

Dim cell As TableCell = CType(list.Parent, TableCell)

declares a TableCell object and sets it to the value the the parent
control
of the cell control cast as a TableCell object. But at this point the
list
object, has no parent control.

What are you actually trying to do?
"glenn" wrote:

> Hi folks,
>
> I have a DropDownList in a DataGrid that is populated from records in a
> database.
>
> I want to add a value that might be a string such as "Select a Company"
> for
> the first item since an OnSelectedIndex event is not fired if you
> select the
> first item.
>
> Does anyone know of an easy way to do this?
>
> I am using the following code to get at the the dropdownlist that is
> inside
> a cell within the datagrid which is placed inside my PageLoad function:
>
> Dim list As DropDownList = New DropDownList ( )
> Dim cell As TableCell = CType(list.Parent, TableCell)
> Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>
> Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
> DropDownList)
>
> ddlCo .Items.Add("Select a Company")
>
> I am getting the infamous "object reference not set to an instance of
> an
> object" on the Dim item line...
>
> Appreciate any replies...
> glenn
>
>
>
>


Apr 25 '06 #7

P: n/a
You're doing it the hard way. Try my suggestion

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
When used without a datasource in a ddl, your example works.

However, when used with a datasource in a ddl, your example does not add
the
dummy item.

My code is as follows:

<asp:DropDownList id="ddlTo"
AppendDataBoundItems="true" runat="server" DataValueField="cname"
AutoPostBack="True" DataSource='<%# GetCompanyNamesForThisProject() %>'
OnSelectedIndexChanged="ddlTo_SelectedIndexChanged ">
<asp:ListItem Selected="True"
Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Thanks again for any tips.
Glenn

"clickon" wrote:
This line
Dim list As DropDownList = New DropDownList ( )

creates a new DdropDownList control called list, it is not in a DataGrid,
it
has no parent or child control, it is not on the page at all, it is
esentially just an object. Therefore list.parent makes no sense unless
there
is a line such as

somecontrol.Controls.Add(list);

Which will place the ddl inside some other control.
If you wanht to add a dummy item to a DropDownList, that is declared on
your
page you can do it as below.

<asp:DropDownList runat="server" ID="MyDDL" AppendDataBoundItems="true">
<asp:ListItem Selected="True"
Text="DummyItem"></asp:ListItem>
</asp:DropDownList>

Because AppendDataBoundItems is set to true then the items from the
database
will be added after the dummy item.

"glenn" wrote:
> I am looking to add a dummy value to my ddl so that when I select the
> first
> real value in the ddl, an OnSelectedIndex event will be fired. The
> value
> might be for example, "Select a Company".
>
> The code I presented I believe needs to be provided in order to find a
> control that is inside a DataGrid. If the ddl was not in a DataGrid I
> could
> simply write something like:
> Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
> but since it is inside the DataGrid, I needed the additonal code.
>
> HTH. If you know of an easier way, please let me know.
> Thanks,
> glenn
>
> "clickon" wrote:
>
> > You code makes no sense for example
> >
> > Dim list As DropDownList = New DropDownList ( )
> >
> > declares and initiates a DropdownList control called list, then
> >
> > Dim cell As TableCell = CType(list.Parent, TableCell)
> >
> > declares a TableCell object and sets it to the value the the parent
> > control
> > of the cell control cast as a TableCell object. But at this point
> > the list
> > object, has no parent control.
> >
> > What are you actually trying to do?
> >
> >
> > "glenn" wrote:
> >
> > > Hi folks,
> > >
> > > I have a DropDownList in a DataGrid that is populated from records
> > > in a
> > > database.
> > >
> > > I want to add a value that might be a string such as "Select a
> > > Company" for
> > > the first item since an OnSelectedIndex event is not fired if you
> > > select the
> > > first item.
> > >
> > > Does anyone know of an easy way to do this?
> > >
> > > I am using the following code to get at the the dropdownlist that
> > > is inside
> > > a cell within the datagrid which is placed inside my PageLoad
> > > function:
> > >
> > > Dim list As DropDownList = New DropDownList ( )
> > > Dim cell As TableCell = CType(list.Parent, TableCell)
> > > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
> > >
> > > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
> > > DropDownList)
> > >
> > > ddlCo .Items.Add("Select a Company")
> > >
> > > I am getting the infamous "object reference not set to an
> > > instance of an
> > > object" on the Dim item line...
> > >
> > > Appreciate any replies...
> > > glenn
> > >
> > >
> > >
> > >

Apr 25 '06 #8

P: n/a
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
The UNION statement only works as shown:

SELECT statement
UNION
SELECT statement

where a statement would be SELECT * FROM TABLE

"Jeff Dillon" wrote:
Just add it to your SQL

SELECT "add a company"
UNION
SELECT CompanyName from Companies

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
>I am looking to add a dummy value to my ddl so that when I select the
>first
> real value in the ddl, an OnSelectedIndex event will be fired. The
> value
> might be for example, "Select a Company".
>
> The code I presented I believe needs to be provided in order to find a
> control that is inside a DataGrid. If the ddl was not in a DataGrid I
> could
> simply write something like:
> Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
> but since it is inside the DataGrid, I needed the additonal code.
>
> HTH. If you know of an easier way, please let me know.
> Thanks,
> glenn
>
> "clickon" wrote:
>
>> You code makes no sense for example
>>
>> Dim list As DropDownList = New DropDownList ( )
>>
>> declares and initiates a DropdownList control called list, then
>>
>> Dim cell As TableCell = CType(list.Parent, TableCell)
>>
>> declares a TableCell object and sets it to the value the the parent
>> control
>> of the cell control cast as a TableCell object. But at this point the
>> list
>> object, has no parent control.
>>
>> What are you actually trying to do?
>>
>>
>> "glenn" wrote:
>>
>> > Hi folks,
>> >
>> > I have a DropDownList in a DataGrid that is populated from records
>> > in a
>> > database.
>> >
>> > I want to add a value that might be a string such as "Select a
>> > Company"
>> > for
>> > the first item since an OnSelectedIndex event is not fired if you
>> > select the
>> > first item.
>> >
>> > Does anyone know of an easy way to do this?
>> >
>> > I am using the following code to get at the the dropdownlist that is
>> > inside
>> > a cell within the datagrid which is placed inside my PageLoad
>> > function:
>> >
>> > Dim list As DropDownList = New DropDownList ( )
>> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >
>> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> > DropDownList)
>> >
>> > ddlCo .Items.Add("Select a Company")
>> >
>> > I am getting the infamous "object reference not set to an instance
>> > of
>> > an
>> > object" on the Dim item line...
>> >
>> > Appreciate any replies...
>> > glenn
>> >
>> >
>> >
>> >


Apr 25 '06 #9

P: n/a
SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.

"Jeff Dillon" wrote:
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
The UNION statement only works as shown:

SELECT statement
UNION
SELECT statement

where a statement would be SELECT * FROM TABLE

"Jeff Dillon" wrote:
Just add it to your SQL

SELECT "add a company"
UNION
SELECT CompanyName from Companies

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
>I am looking to add a dummy value to my ddl so that when I select the
>first
> real value in the ddl, an OnSelectedIndex event will be fired. The
> value
> might be for example, "Select a Company".
>
> The code I presented I believe needs to be provided in order to find a
> control that is inside a DataGrid. If the ddl was not in a DataGrid I
> could
> simply write something like:
> Dim ddl as DropDownList = e.Item.Cells(2).FindControl("ddlCompany")
> but since it is inside the DataGrid, I needed the additonal code.
>
> HTH. If you know of an easier way, please let me know.
> Thanks,
> glenn
>
> "clickon" wrote:
>
>> You code makes no sense for example
>>
>> Dim list As DropDownList = New DropDownList ( )
>>
>> declares and initiates a DropdownList control called list, then
>>
>> Dim cell As TableCell = CType(list.Parent, TableCell)
>>
>> declares a TableCell object and sets it to the value the the parent
>> control
>> of the cell control cast as a TableCell object. But at this point the
>> list
>> object, has no parent control.
>>
>> What are you actually trying to do?
>>
>>
>> "glenn" wrote:
>>
>> > Hi folks,
>> >
>> > I have a DropDownList in a DataGrid that is populated from records
>> > in a
>> > database.
>> >
>> > I want to add a value that might be a string such as "Select a
>> > Company"
>> > for
>> > the first item since an OnSelectedIndex event is not fired if you
>> > select the
>> > first item.
>> >
>> > Does anyone know of an easy way to do this?
>> >
>> > I am using the following code to get at the the dropdownlist that is
>> > inside
>> > a cell within the datagrid which is placed inside my PageLoad
>> > function:
>> >
>> > Dim list As DropDownList = New DropDownList ( )
>> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >
>> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> > DropDownList)
>> >
>> > ddlCo .Items.Add("Select a Company")
>> >
>> > I am getting the infamous "object reference not set to an instance
>> > of
>> > an
>> > object" on the Dim item line...
>> >
>> > Appreciate any replies...
>> > glenn
>> >
>> >
>> >
>> >


Apr 25 '06 #10

P: n/a
YES IT IS!! Did you try it? What do you mean "not valid"? Of course it is.

I ran this in Query Analyzer, and it works just fine. We use it in our DDL
scripts all the time.

Please try my suggestions first next time. We do this all the time at this
company to populate the first item in our lists..

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.

"Jeff Dillon" wrote:
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
> The UNION statement only works as shown:
>
> SELECT statement
> UNION
> SELECT statement
>
> where a statement would be SELECT * FROM TABLE
>
> "Jeff Dillon" wrote:
>
>> Just add it to your SQL
>>
>> SELECT "add a company"
>> UNION
>> SELECT CompanyName from Companies
>>
>> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> news:68**********************************@microsof t.com...
>> >I am looking to add a dummy value to my ddl so that when I select the
>> >first
>> > real value in the ddl, an OnSelectedIndex event will be fired. The
>> > value
>> > might be for example, "Select a Company".
>> >
>> > The code I presented I believe needs to be provided in order to find
>> > a
>> > control that is inside a DataGrid. If the ddl was not in a DataGrid
>> > I
>> > could
>> > simply write something like:
>> > Dim ddl as DropDownList =
>> > e.Item.Cells(2).FindControl("ddlCompany")
>> > but since it is inside the DataGrid, I needed the additonal code.
>> >
>> > HTH. If you know of an easier way, please let me know.
>> > Thanks,
>> > glenn
>> >
>> > "clickon" wrote:
>> >
>> >> You code makes no sense for example
>> >>
>> >> Dim list As DropDownList = New DropDownList ( )
>> >>
>> >> declares and initiates a DropdownList control called list, then
>> >>
>> >> Dim cell As TableCell = CType(list.Parent, TableCell)
>> >>
>> >> declares a TableCell object and sets it to the value the the parent
>> >> control
>> >> of the cell control cast as a TableCell object. But at this point
>> >> the
>> >> list
>> >> object, has no parent control.
>> >>
>> >> What are you actually trying to do?
>> >>
>> >>
>> >> "glenn" wrote:
>> >>
>> >> > Hi folks,
>> >> >
>> >> > I have a DropDownList in a DataGrid that is populated from
>> >> > records
>> >> > in a
>> >> > database.
>> >> >
>> >> > I want to add a value that might be a string such as "Select a
>> >> > Company"
>> >> > for
>> >> > the first item since an OnSelectedIndex event is not fired if you
>> >> > select the
>> >> > first item.
>> >> >
>> >> > Does anyone know of an easy way to do this?
>> >> >
>> >> > I am using the following code to get at the the dropdownlist that
>> >> > is
>> >> > inside
>> >> > a cell within the datagrid which is placed inside my PageLoad
>> >> > function:
>> >> >
>> >> > Dim list As DropDownList = New DropDownList ( )
>> >> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >> >
>> >> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> >> > DropDownList)
>> >> >
>> >> > ddlCo .Items.Add("Select a Company")
>> >> >
>> >> > I am getting the infamous "object reference not set to an
>> >> > instance
>> >> > of
>> >> > an
>> >> > object" on the Dim item line...
>> >> >
>> >> > Appreciate any replies...
>> >> > glenn
>> >> >
>> >> >
>> >> >
>> >> >
>>
>>
>>


Apr 25 '06 #11

P: n/a
Paste the following code in query analyzer, and use the Pubs database:

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

You can also do the same thing in Access..I just tested it

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.

"Jeff Dillon" wrote:
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
> The UNION statement only works as shown:
>
> SELECT statement
> UNION
> SELECT statement
>
> where a statement would be SELECT * FROM TABLE
>
> "Jeff Dillon" wrote:
>
>> Just add it to your SQL
>>
>> SELECT "add a company"
>> UNION
>> SELECT CompanyName from Companies
>>
>> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> news:68**********************************@microsof t.com...
>> >I am looking to add a dummy value to my ddl so that when I select the
>> >first
>> > real value in the ddl, an OnSelectedIndex event will be fired. The
>> > value
>> > might be for example, "Select a Company".
>> >
>> > The code I presented I believe needs to be provided in order to find
>> > a
>> > control that is inside a DataGrid. If the ddl was not in a DataGrid
>> > I
>> > could
>> > simply write something like:
>> > Dim ddl as DropDownList =
>> > e.Item.Cells(2).FindControl("ddlCompany")
>> > but since it is inside the DataGrid, I needed the additonal code.
>> >
>> > HTH. If you know of an easier way, please let me know.
>> > Thanks,
>> > glenn
>> >
>> > "clickon" wrote:
>> >
>> >> You code makes no sense for example
>> >>
>> >> Dim list As DropDownList = New DropDownList ( )
>> >>
>> >> declares and initiates a DropdownList control called list, then
>> >>
>> >> Dim cell As TableCell = CType(list.Parent, TableCell)
>> >>
>> >> declares a TableCell object and sets it to the value the the parent
>> >> control
>> >> of the cell control cast as a TableCell object. But at this point
>> >> the
>> >> list
>> >> object, has no parent control.
>> >>
>> >> What are you actually trying to do?
>> >>
>> >>
>> >> "glenn" wrote:
>> >>
>> >> > Hi folks,
>> >> >
>> >> > I have a DropDownList in a DataGrid that is populated from
>> >> > records
>> >> > in a
>> >> > database.
>> >> >
>> >> > I want to add a value that might be a string such as "Select a
>> >> > Company"
>> >> > for
>> >> > the first item since an OnSelectedIndex event is not fired if you
>> >> > select the
>> >> > first item.
>> >> >
>> >> > Does anyone know of an easy way to do this?
>> >> >
>> >> > I am using the following code to get at the the dropdownlist that
>> >> > is
>> >> > inside
>> >> > a cell within the datagrid which is placed inside my PageLoad
>> >> > function:
>> >> >
>> >> > Dim list As DropDownList = New DropDownList ( )
>> >> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >> >
>> >> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> >> > DropDownList)
>> >> >
>> >> > ddlCo .Items.Add("Select a Company")
>> >> >
>> >> > I am getting the infamous "object reference not set to an
>> >> > instance
>> >> > of
>> >> > an
>> >> > object" on the Dim item line...
>> >> >
>> >> > Appreciate any replies...
>> >> > glenn
>> >> >
>> >> >
>> >> >
>> >> >
>>
>>
>>


Apr 25 '06 #12

P: n/a
Jeff,

It works in SQL Server but not in Access. Currently we are working with
Access. I get a message box in Access upon running a SQL Query from the
Northwind.mdb file that states "Query Input must contain at least one table
or query".

I used your SQL syntax and changed it for the northwind.mdb file

SELECT 'Select a Title'
UNION ALL
SELECT TITLE FROM Employees

Am I missing something. I am unsure how you got it to work with Access.
Pubs is a SQL Server only db. Please do not use all caps in your response.
It sounds like yelling and is not warranted.

Thanks for your help so far. I truly appreciate it. I will investigate the
UNION ALL in Access unless you have anything to add.

glenn

"Jeff Dillon" wrote:
Paste the following code in query analyzer, and use the Pubs database:

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

You can also do the same thing in Access..I just tested it

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.

"Jeff Dillon" wrote:
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
> The UNION statement only works as shown:
>
> SELECT statement
> UNION
> SELECT statement
>
> where a statement would be SELECT * FROM TABLE
>
> "Jeff Dillon" wrote:
>
>> Just add it to your SQL
>>
>> SELECT "add a company"
>> UNION
>> SELECT CompanyName from Companies
>>
>> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> news:68**********************************@microsof t.com...
>> >I am looking to add a dummy value to my ddl so that when I select the
>> >first
>> > real value in the ddl, an OnSelectedIndex event will be fired. The
>> > value
>> > might be for example, "Select a Company".
>> >
>> > The code I presented I believe needs to be provided in order to find
>> > a
>> > control that is inside a DataGrid. If the ddl was not in a DataGrid
>> > I
>> > could
>> > simply write something like:
>> > Dim ddl as DropDownList =
>> > e.Item.Cells(2).FindControl("ddlCompany")
>> > but since it is inside the DataGrid, I needed the additonal code.
>> >
>> > HTH. If you know of an easier way, please let me know.
>> > Thanks,
>> > glenn
>> >
>> > "clickon" wrote:
>> >
>> >> You code makes no sense for example
>> >>
>> >> Dim list As DropDownList = New DropDownList ( )
>> >>
>> >> declares and initiates a DropdownList control called list, then
>> >>
>> >> Dim cell As TableCell = CType(list.Parent, TableCell)
>> >>
>> >> declares a TableCell object and sets it to the value the the parent
>> >> control
>> >> of the cell control cast as a TableCell object. But at this point
>> >> the
>> >> list
>> >> object, has no parent control.
>> >>
>> >> What are you actually trying to do?
>> >>
>> >>
>> >> "glenn" wrote:
>> >>
>> >> > Hi folks,
>> >> >
>> >> > I have a DropDownList in a DataGrid that is populated from
>> >> > records
>> >> > in a
>> >> > database.
>> >> >
>> >> > I want to add a value that might be a string such as "Select a
>> >> > Company"
>> >> > for
>> >> > the first item since an OnSelectedIndex event is not fired if you
>> >> > select the
>> >> > first item.
>> >> >
>> >> > Does anyone know of an easy way to do this?
>> >> >
>> >> > I am using the following code to get at the the dropdownlist that
>> >> > is
>> >> > inside
>> >> > a cell within the datagrid which is placed inside my PageLoad
>> >> > function:
>> >> >
>> >> > Dim list As DropDownList = New DropDownList ( )
>> >> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >> >
>> >> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> >> > DropDownList)
>> >> >
>> >> > ddlCo .Items.Add("Select a Company")
>> >> >
>> >> > I am getting the infamous "object reference not set to an
>> >> > instance
>> >> > of
>> >> > an
>> >> > object" on the Dim item line...
>> >> >
>> >> > Appreciate any replies...
>> >> > glenn
>> >> >
>> >> >
>> >> >
>> >> >
>>
>>
>>


Apr 25 '06 #13

P: n/a
Jeff,
I used the following SQL syntax and it works now in Access

SELECT DISTINCT 'Select a Title' FROM Employees
UNION ALL SELECT TITLE FROM Employees;

Thanks again Jeff. You put me on the right track. Doing this in the
ASP.NET code would have been a real bear.

glenn

"Jeff Dillon" wrote:
Paste the following code in query analyzer, and use the Pubs database:

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

You can also do the same thing in Access..I just tested it

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
SELECT 'Select an author' is not a valid SQL statement...

I want to add a selection to the top of my ddl. That is the issue.

"Jeff Dillon" wrote:
WRONG

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

works fine

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
> The UNION statement only works as shown:
>
> SELECT statement
> UNION
> SELECT statement
>
> where a statement would be SELECT * FROM TABLE
>
> "Jeff Dillon" wrote:
>
>> Just add it to your SQL
>>
>> SELECT "add a company"
>> UNION
>> SELECT CompanyName from Companies
>>
>> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> news:68**********************************@microsof t.com...
>> >I am looking to add a dummy value to my ddl so that when I select the
>> >first
>> > real value in the ddl, an OnSelectedIndex event will be fired. The
>> > value
>> > might be for example, "Select a Company".
>> >
>> > The code I presented I believe needs to be provided in order to find
>> > a
>> > control that is inside a DataGrid. If the ddl was not in a DataGrid
>> > I
>> > could
>> > simply write something like:
>> > Dim ddl as DropDownList =
>> > e.Item.Cells(2).FindControl("ddlCompany")
>> > but since it is inside the DataGrid, I needed the additonal code.
>> >
>> > HTH. If you know of an easier way, please let me know.
>> > Thanks,
>> > glenn
>> >
>> > "clickon" wrote:
>> >
>> >> You code makes no sense for example
>> >>
>> >> Dim list As DropDownList = New DropDownList ( )
>> >>
>> >> declares and initiates a DropdownList control called list, then
>> >>
>> >> Dim cell As TableCell = CType(list.Parent, TableCell)
>> >>
>> >> declares a TableCell object and sets it to the value the the parent
>> >> control
>> >> of the cell control cast as a TableCell object. But at this point
>> >> the
>> >> list
>> >> object, has no parent control.
>> >>
>> >> What are you actually trying to do?
>> >>
>> >>
>> >> "glenn" wrote:
>> >>
>> >> > Hi folks,
>> >> >
>> >> > I have a DropDownList in a DataGrid that is populated from
>> >> > records
>> >> > in a
>> >> > database.
>> >> >
>> >> > I want to add a value that might be a string such as "Select a
>> >> > Company"
>> >> > for
>> >> > the first item since an OnSelectedIndex event is not fired if you
>> >> > select the
>> >> > first item.
>> >> >
>> >> > Does anyone know of an easy way to do this?
>> >> >
>> >> > I am using the following code to get at the the dropdownlist that
>> >> > is
>> >> > inside
>> >> > a cell within the datagrid which is placed inside my PageLoad
>> >> > function:
>> >> >
>> >> > Dim list As DropDownList = New DropDownList ( )
>> >> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >> >
>> >> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> >> > DropDownList)
>> >> >
>> >> > ddlCo .Items.Add("Select a Company")
>> >> >
>> >> > I am getting the infamous "object reference not set to an
>> >> > instance
>> >> > of
>> >> > an
>> >> > object" on the Dim item line...
>> >> >
>> >> > Appreciate any replies...
>> >> > glenn
>> >> >
>> >> >
>> >> >
>> >> >
>>
>>
>>


Apr 25 '06 #14

P: n/a
I was yelling. Got a problem with that?

SELECT DISTINCT 'Select a Title' FROM Employees;
UNION ALL
SELECT TITLE FROM Employees;

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
Jeff,

It works in SQL Server but not in Access. Currently we are working with
Access. I get a message box in Access upon running a SQL Query from the
Northwind.mdb file that states "Query Input must contain at least one
table
or query".

I used your SQL syntax and changed it for the northwind.mdb file

SELECT 'Select a Title'
UNION ALL
SELECT TITLE FROM Employees

Am I missing something. I am unsure how you got it to work with Access.
Pubs is a SQL Server only db. Please do not use all caps in your
response.
It sounds like yelling and is not warranted.

Thanks for your help so far. I truly appreciate it. I will investigate
the
UNION ALL in Access unless you have anything to add.

glenn

"Jeff Dillon" wrote:
Paste the following code in query analyzer, and use the Pubs database:

SELECT 'Select an author'
UNION ALL
SELECT au_lname FROM Authors

You can also do the same thing in Access..I just tested it

"glenn" <gl***@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
> SELECT 'Select an author' is not a valid SQL statement...
>
> I want to add a selection to the top of my ddl. That is the issue.
>
> "Jeff Dillon" wrote:
>
>> WRONG
>>
>> SELECT 'Select an author'
>> UNION ALL
>> SELECT au_lname FROM Authors
>>
>> works fine
>>
>> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> news:A6**********************************@microsof t.com...
>> > The UNION statement only works as shown:
>> >
>> > SELECT statement
>> > UNION
>> > SELECT statement
>> >
>> > where a statement would be SELECT * FROM TABLE
>> >
>> > "Jeff Dillon" wrote:
>> >
>> >> Just add it to your SQL
>> >>
>> >> SELECT "add a company"
>> >> UNION
>> >> SELECT CompanyName from Companies
>> >>
>> >> "glenn" <gl***@discussions.microsoft.com> wrote in message
>> >> news:68**********************************@microsof t.com...
>> >> >I am looking to add a dummy value to my ddl so that when I select
>> >> >the
>> >> >first
>> >> > real value in the ddl, an OnSelectedIndex event will be fired.
>> >> > The
>> >> > value
>> >> > might be for example, "Select a Company".
>> >> >
>> >> > The code I presented I believe needs to be provided in order to
>> >> > find
>> >> > a
>> >> > control that is inside a DataGrid. If the ddl was not in a
>> >> > DataGrid
>> >> > I
>> >> > could
>> >> > simply write something like:
>> >> > Dim ddl as DropDownList =
>> >> > e.Item.Cells(2).FindControl("ddlCompany")
>> >> > but since it is inside the DataGrid, I needed the additonal code.
>> >> >
>> >> > HTH. If you know of an easier way, please let me know.
>> >> > Thanks,
>> >> > glenn
>> >> >
>> >> > "clickon" wrote:
>> >> >
>> >> >> You code makes no sense for example
>> >> >>
>> >> >> Dim list As DropDownList = New DropDownList ( )
>> >> >>
>> >> >> declares and initiates a DropdownList control called list, then
>> >> >>
>> >> >> Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> >>
>> >> >> declares a TableCell object and sets it to the value the the
>> >> >> parent
>> >> >> control
>> >> >> of the cell control cast as a TableCell object. But at this
>> >> >> point
>> >> >> the
>> >> >> list
>> >> >> object, has no parent control.
>> >> >>
>> >> >> What are you actually trying to do?
>> >> >>
>> >> >>
>> >> >> "glenn" wrote:
>> >> >>
>> >> >> > Hi folks,
>> >> >> >
>> >> >> > I have a DropDownList in a DataGrid that is populated from
>> >> >> > records
>> >> >> > in a
>> >> >> > database.
>> >> >> >
>> >> >> > I want to add a value that might be a string such as "Select a
>> >> >> > Company"
>> >> >> > for
>> >> >> > the first item since an OnSelectedIndex event is not fired if
>> >> >> > you
>> >> >> > select the
>> >> >> > first item.
>> >> >> >
>> >> >> > Does anyone know of an easy way to do this?
>> >> >> >
>> >> >> > I am using the following code to get at the the dropdownlist
>> >> >> > that
>> >> >> > is
>> >> >> > inside
>> >> >> > a cell within the datagrid which is placed inside my PageLoad
>> >> >> > function:
>> >> >> >
>> >> >> > Dim list As DropDownList = New DropDownList ( )
>> >> >> > Dim cell As TableCell = CType(list.Parent, TableCell)
>> >> >> > Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
>> >> >> >
>> >> >> > Dim ddlCo As DropDownList = CType(item.FindControl("ddlCo"),
>> >> >> > DropDownList)
>> >> >> >
>> >> >> > ddlCo .Items.Add("Select a Company")
>> >> >> >
>> >> >> > I am getting the infamous "object reference not set to an
>> >> >> > instance
>> >> >> > of
>> >> >> > an
>> >> >> > object" on the Dim item line...
>> >> >> >
>> >> >> > Appreciate any replies...
>> >> >> > glenn
>> >> >> >
>> >> >> >
>> >> >> >
>> >> >> >
>> >>
>> >>
>> >>
>>
>>
>>


Apr 25 '06 #15

P: n/a
Here is my solution. Just modifiy the DataBound event of the
DropDownList. This example add an "All" default to the Candidate
DropDownList :

protected void DropDownListCandidate_DataBound(object sender,
EventArgs e)
{
DropDownListCandidate.Items.Insert(0, new
ListItem("All").ToString());
}
*** Sent via Developersdex http://www.developersdex.com ***
May 8 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.