Professional Web Applications Themes

asp.net datagrid with textbox control showing error - Brainstorming Area

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 ...

  1. #1

    Default 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
    Last edited by vinodv68; April 21st at 06:39 PM.
    vinodv68 is offline Junior Member
    Join Date
    Apr 2013
    Location
    Trivandrum, Kerala, India
    Posts
    1

Similar Threads

  1. Changing text to a textbox control in row 5 of a datagrid
    By Yusuf in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: March 14th, 04:14 PM
  2. Replies: 2
    Last Post: September 30th, 06:41 PM
  3. textbox inside datagrid control
    By Calvin in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: October 23rd, 12:40 PM
  4. Hiding and Showing Columns in the DataGrid Control...
    By Amber in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: October 11th, 07:17 PM
  5. Replies: 0
    Last Post: July 16th, 06:51 PM

Bookmarks

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