Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a gridview with some textboxes where user can input.
but when i run it, i get this error

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


Markup

'''
<asp:Button ID="btnAdd" runat="server" Font-Bold="True" Height="42px" OnClick="btnAdd_Click" Text ="Add Record" Width="116px" />
'''

Code Behind (VB)
'''
Protected Sub Gridview1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

GridView1.PageIndex = e.NewPageIndex
bindGrid()

End Sub

Protected Sub btnAdd_Click(sender As Object, e As EventArgs)

Dim btn As Button = DirectCast(sender, Button)
Dim GrdRow As GridViewRow = DirectCast(btn.Parent.Parent, GridViewRow)

Dim txtTGID As TextBox = DirectCast(GrdRow.Cells(0).FindControl("txtReturnTGID"), TextBox)
Dim txtPassNo As TextBox = DirectCast(GrdRow.Cells(0).FindControl("txtPassNo"), TextBox)
Dim txtTouristName As TextBox = DirectCast(GrdRow.Cells(0).FindControl("txtPassName"), TextBox)

Using conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("CommissionConnectionString2").ConnectionString)
Using cmd1 As New SqlCommand("SELECT * FROM dbo.Tour_Detail")

With cmd1
.Connection = conn1 : conn1.Open()

.CommandText = "INSERT INTO dbo.Tour_detail (Tour_Group_ID, Passport_No, Tourist_Name) VALUES (@Tour_Group_ID, @Passport_No, @ Tourist_Name)"
.Parameters.AddWithValue("@Tour_Group_ID", Trim(txtReturnTGID.Text))
.Parameters.AddWithValue("@Passport_No", Trim(txtPassNo.Text))
.Parameters.AddWithValue("@Tourist_Name", Trim(txtPassName.Text))

.ExecuteNonQuery()
End With

End Using

End Using
bindGrid()
End Sub
'''
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900