Professional Web Applications Themes

Reply to Thread

Post a reply to the thread: Question asp.net datagrid with textbox control showing error

Your Message

Click here to log in

 

Send Trackbacks to (Separate multiple URLs with spaces)

You may choose an icon for your message from this list

Additional Options

Rate Thread

You may rate this thread from 1-star (Terrible) to 5-stars (Excellent) if you wish to do so.

Topic Review (Newest First)

  • April 21st, 06:35 PM
    vinodv68

    asp.net datagrid with textbox control showing error

    Pls Help,

    I have created a datagrid containing a textbox carring values of field "division" from table netresult_II, through coding. but The following line of code

    --------------------------------------------------------
    For i = 0 To 5
    Dim MytextBox As TextBox = CType(DataGrid1.Items(i).Cells(0).Controls(0), TextBox)
    Label3.Text = Label3.Text & " " & MytextBox.Text
    Next i
    --------------------------------------------------------

    works fine in show_click

    but when the same code is placed in save_click it displays an error saying

    "Specified argument was out of the range of valid values. Parameter name: index "

    can you pls help me. I have only just started learning asp.net

    the full code is placed here for reference

    Private Sub Show_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show.Click
    Dim SqlConn As SqlConnection = New SqlConnection("server = VINOD-40BD3D4B9; database = Resysis; user id = result; password = result")
    SqlConn.Open()
    Dim SqlDA As SqlDataAdapter
    Dim ds As DataSet
    Dim dt As DataTable
    Dim com As SqlCommand
    Dim str As String

    str = "select Regno, Cand_Name, Division from netresult_II where school_code = '" & Scode.Text.Trim & "' order by regno"
    com = New SqlCommand(str, SqlConn)
    ds = New DataSet
    SqlDA = New SqlDataAdapter(com)
    SqlDA.Fill(ds, "Netresult_II")
    dt = ds.Tables("Netresult_II")
    DataGrid1.DataSource = dt

    Dim tcol As New TemplateColumn
    With tcol
    .ItemTemplate = New DynamicItemTemplate
    .HeaderText = "Division"
    End With
    DataGrid1.Columns.Add(tcol)
    DataGrid1.DataBind()
    DataGrid1.CellPadding = 5
    Dim i As Integer

    For i = 0 To 5
    Dim MytextBox As TextBox = CType(DataGrid1.Items(i).Cells(0).Controls(0), TextBox)
    Label3.Text = Label3.Text & " " & MytextBox.Text
    Next i
    SqlConn.Close()
    End Sub

    Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

    Dim i As Integer

    For i = 1 To 5
    Dim MytextBox As TextBox = CType(DataGrid1.Items(i).Cells(0).Controls(0), TextBox)
    Label3.Text = Label3.Text & " " & MytextBox.Text
    Next

    End Sub

    End Class

    Public Class DynamicItemTemplate
    Implements ITemplate

    Public Overridable Overloads Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
    Dim oTextBox As TextBox = New TextBox
    oTextBox.Width = oTextBox.Width.Point(20)
    AddHandler oTextBox.DataBinding, AddressOf BindTextBox
    container.Controls.Add(oTextBox)
    End Sub

    Public Sub BindTextBox(ByVal sender As Object, ByVal e As EventArgs)
    Dim oTextBox As TextBox = CType(sender, TextBox)
    Dim container As DataGridItem = CType(oTextBox.NamingContainer, DataGridItem)
    oTextBox.Text = container.DataItem("Division")
    End Sub
    End Class

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139