Hi All Experts,

Actually I am VB.Net Windows Application Developer Now i am going an ASP.Net Application. So, the Problem is that in Windows Application i Code this Project like given Below Actually i am Using SQL Server as DataBase and Store Procedure as Query.
Becuase i had tried to control the ASP.Net Data Grid with Loop and i Failed to Control it with this method so can any one Help me.
(Can any one Help that how can i Solve this Problem in ASP.Net)

Private Sub btn_Ok_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btn_Ok.Click
Fill_dataset("Sel_MovNames", "Tbl_MovNames", Convert.toint32(cmb_MovID.SelectedValue))
      For rowcnt As Integer = 0 To ds.Tables("Tbl_MovNames").Rows.Count - 1
        dg_Movies.Rows.Add()
        dg_Movies.Item("col_SrNo", dg_Movies.RowCount - 2).Value = rowcnt + 1
        dg_Movies.Item("col_MovID", dg_Movies.RowCount - 2).Value = ds.Tables("Tbl_MovNames").Rows(rowcnt)("MovID").ToString
        dg_Movies.Item("col_MovName", dg_Movies.RowCount - 2).Value = ds.Tables("Tbl_MovNames").Rows(rowcnt)("MovName").ToString
        dg_Movies.Item("col_MovYear", dg_Movies.RowCount - 2).Value = ds.Tables("Tbl_MovNames").Rows(rowcnt)("MovYear").ToString
        dg_Movies.Item("col_MovViews", dg_Movies.RowCount - 2).Value = ds.Tables("Tbl_MovNames").Rows(rowcnt)("Views").ToString
     Next
 End Sub
 
Private Sub Fill_dataset(ByVal SPName As String, ByVal TableName As String, ByVal arg1 As Integer)
        SPName = SPName & " '" & arg1 & "'"
        myAdapter = New SqlDataAdapter(SPName, SQLCon)
        myAdapter.Fill(ds, TableName)
        ds.Tables(TableName).Clear()
        myAdapter.Fill(ds, TableName)
End Sub

Dani AI

Generated

Moving from WinForms to ASP.NET means changing mindset: you don't add rows to a web grid the way you do in a desktop DataGridView. Server grids are meant to be data-bound. is on the right track — bind a DataTable/DataView to the grid and let the control render rows. Below is a safe, practical pattern for calling a stored procedure with a parameter, filling a DataTable, and binding it from a server-side button click.

' Safe helper: returns a DataTable from the stored proc
Private Function GetMoviesById(movId As Integer) As DataTable
    Dim dt As New DataTable()
    Dim cs As String = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString

    Using cn As New SqlConnection(cs)
        Using cmd As New SqlCommand("Sel_MovNames", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@MovID", SqlDbType.Int).Value = movId

            Using da As New SqlDataAdapter(cmd)
                da.Fill(dt)
            End Using
        End Using
    End Using

    Return dt
End Function

Bind the result in your button handler and call DataBind:

Dim dt As DataTable = GetMoviesById(CInt(cmb_MovID.SelectedValue))
GridView1.DataSource = dt
GridView1.DataBind()

To show a serial number you can either add a computed column before binding or set it in RowDataBound. Example (RowDataBound):

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Cells(0).Text = (e.Row.RowIndex + 1).ToString()
    End If
End Sub

Troubleshooting notes: never build SP calls by concatenating values (use parameters); avoid filling/clearing the same DataTable repeatedly; rebind explicitly when needed (watch Page_Load and IsPostBack logic); dispose connections with Using; set DataKeyNames if you need the MovID later. This replaces the WinForms row-add loop approach and keeps the page robust and safe for ASP.NET.

I guess using the following will be much easier.

dg_Movies.DataSource = ds.Tables("Tbl_MovNames").DefaultView()
dg_Movies.DataBind()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.