473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding dummy item to DropDownList in a DataGrid

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
15 3069
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brian Greiwe | last post by:
I posted this in the datagrid forum but got no bites, so I thought I'd post it here as well for some help.... I've created a datagrid with 1 edititemtemplate column. When the user clicks...
2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
2
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
3
by: Tim::.. | last post by:
Can someone please tell me how I go about preselecting an item in a drop drown list when I click the Edit Command in a datagrid? I have tried the following but it doesn't work for me! I would...
0
by: Luis Esteban Valencia | last post by:
Please help me if possible Its only adding a row. When I click again it steps into the function but doesnt add it to the page. Private Sub LinkButton2_Click(ByVal sender As System.Object,...
3
by: Sam | last post by:
Hi All, I'm having troubles with populating data to the dropdownlist in a datagrid and I just don't know what causes it. I have been struggling for 2 days now Would some one give me a hand? ...
6
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.