Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:13:44+00:00 2026-06-18T05:13:44+00:00

We have a dropdownlist in gridview that gets populated from the database. There is

  • 0

We have a dropdownlist in gridview that gets populated from the database.

There is a also a textbox next to this dropdownlist.

If the option the user is looking for isn’t in the dropdown, enter that value into the textbox and when submitted to the database, will now become in the dropdownlist.

I am running into the following error:

Unable to cast object of type 'ASP.addtoroster_aspx' to type 'System.Web.UI.WebControls.GridViewRow'.

The error is on this line:

Dim parentRow As GridViewRow = DirectCast(button.NamingContainer, GridViewRow)

I believe this error occurs because I am using an imagebutton on the markup to submit to the database.

<asp:ImageButton ID="btnSave" runat="server" ImageUrl="images/save.png" 
                                             onmouseout="this.src='images/save.png'" 
                                             onmouseover="this.src='images/save.png'"

 OnClick="btnSave_Click" alt="Save Data" /> 

Any idea how to resolve this?

Even though this is vb, I welcome solution in c# if available.

Thank you!

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Try
    SetRowData()
    Dim table As DataTable = TryCast(ViewState("CurrentTable"), DataTable)

    If table IsNot Nothing Then
        For Each row As DataRow In table.Rows
            Dim txLName As String = TryCast(row.ItemArray(1), String)
            Dim txName As String = TryCast(row.ItemArray(2), String)
            Dim txEmail As String = TryCast(row.ItemArray(3), String)
            Dim txRole As String = TryCast(row.ItemArray(4), String)
            Dim txPhone As String = TryCast(row.ItemArray(5), String)
            Dim drpEmpl As String = TryCast(row.ItemArray(6), String)
            Dim txVIP As String = TryCast(row.ItemArray(7), String)
            Dim drpLCB As String = TryCast(row.ItemArray(8), String)

            'Find the button
            Dim button As Button = DirectCast(sender, Button)
            'Find parent row
            Dim parentRow As GridViewRow = DirectCast(button.NamingContainer, GridViewRow)
            'find DropDownlist and textbox
            Dim ddl As DropDownList = TryCast(parentRow.FindControl("txtLoginName"), DropDownList)
            Dim txtNewUser As TextBox = TryCast(parentRow.FindControl("txtNewUser"), TextBox)
            If txtNewUser IsNot Nothing AndAlso ddl IsNot Nothing Then
                'add new listitem here
                Dim customItem As New ListItem(txtNewUser.Text, txtNewUser.Text)
                ddl.Items.Add(customItem)
            End If

            Dim ddlvalue As String = ""
            Dim idx As Integer = grvStudentDetails.EditIndex

            If drpEmpl = "Other" Then
                ddlvalue = DirectCast(grvStudentDetails.FindControl("txtOther"), TextBox).Text

                '   Else
                '      ddlvalue = drpEmpl
            End If


            If txLName IsNot Nothing OrElse txLName IsNot Nothing OrElse txEmail IsNot Nothing OrElse txRole IsNot Nothing OrElse txPhone IsNot Nothing OrElse drpEmpl IsNot Nothing OrElse txVIP IsNot Nothing OrElse drpLCB IsNot Nothing Then
                ' Response.Write(String.Format("{0} {1} {2} {3} {4} {5} {6} {7} <br/>", txLName, txName, txEmail, txRole, txPhone, drpEmpl, txVIP, drpLCB))

                'Try
                Dim dateentered As String = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
                'Response.Write(dateentered)
                'Response.End()

                Dim s As String
                Dim count As Integer

                'If LoginName already exists, alert user
                s = "SELECT Count(*) FROM Employee_Roster WHERE login_id = " & txLName
                'Response.Write(s)
                'Response.End()
                Dim connSt As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
                Dim connc As New OleDbConnection(connSt)
                Dim cmdc As New OleDbCommand(s, connc)
                'cmdc.Parameters.AddWithValue("login_id", txtLoginName.SelectedValue)
                connc.Open()
                ' cmdc.ExecuteNonQuery()
                count = cmdc.ExecuteScalar()


                '   Now let's see if we found existing record
                If count > 0 Then
                    'Display some feedback to the user to let them know it was processed
                    lblResult.ForeColor = System.Drawing.Color.Green
                    lblResult.Text = "User already is in the Excel Sheet!"
                Else
                    s = "INSERT INTO Employee_Roster(login_id, FullName, Email_Address, Role_Dept,Phone,Employer,VP,entryDate,Notes) VALUES "
                    s += "('" & txLName & "', '" & txName & "', '" & txEmail & "', '" & txRole & "', '" & txPhone & "', '" & ddlvalue & "','" & txVIP & "','" & dateentered & "', '" & drpLCB & "')"
                    Response.Write(s)
                    Response.End()
                    Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
                    Dim conn As New OleDbConnection(connStr)
                    Dim cmd As New OleDbCommand(s, conn)
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()


                    'Display some feedback to the user to let them know it was processed
                    lblResult.ForeColor = System.Drawing.Color.Green
                    lblResult.Text = "Record successfully saved!"

                    'Clear the form
                    txLName = ""
                    txLName = ""
                    txEmail = ""
                    txRole = ""
                    txPhone = ""
                    txVIP = ""
                End If
                ' Catch

                'If the message failed at some point, let the user know
                lblResult.ForeColor = System.Drawing.Color.Red
                lblResult.Text = "Your record failed to save, please try again."

                ' End Try

            End If
        Next
    End If
    '  Catch ex As Exception
    'Throw New Exception(ex.Message)
    ' End Try
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:13:45+00:00Added an answer on June 18, 2026 at 5:13 am
    Dim parentRow As GridViewRow = DirectCast(button.NamingContainer, GridViewRow)
    

    this button is out side of Gridview and this button belongs to ASP.addtoroster_aspx

    but when you say button.NamingContainer it points ASP.addtoroster_aspx as it holds that

    button,so casting is failing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview that imports data from a remote database, this is working
I have a dropdownlist inside a gridview, that is loaded from a datasource when
I have a gridview in an updatepanel where also my dropdownlist is. From the
I have a DropDownList with values that I get from a SQL database. Depending
hi I have this gridview like this. <asp:DropDownList ID=triggerDropDown runat=server AutoPostBack=true onselectedindexchanged=triggerDropDown_SelectedIndexChanged> <asp:GridView ID=myGridView
Background I have a GridView that builds a table using an ObjectDataSource . This
I have a GridView that's bound to a DropDownList. The GridView is updated by
I have a DropDownList in a GridView EditItemTemplate. The ddl is to be populated
i have a gridview that has paging enabled. i also have a dropdown on
I have a RadGrid (Gridview) that has the option to edit and insert new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.