I have the following code...
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.RowUpdating
Dim SocioNumInfo As Integer = CInt(GridView1.Rows(GridView1.SelectedIndex).Cells(4).Text)
MsgBox(SocioNumInfo.ToString)
...
End Sub
Now, this code should read the cell, but it gives me the following error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The MsgBox is just there for me to check if the data is being read, at the end of the day it should turn that into a parameter so I can add to the DB... but still.. nope nothing. Is that the correct code to read directly from a cell in a choosen row? In the "Protected Sub" area I already tried SelectedIndexChanging and RowEditing, etc... and still nothing. Still throws the error at me.
If I try
Dim SocioNumInfo As String = CStr(GridView1.Rows(GridView1.SelectedRow).Cells(4).Text) it gives me a "cannot be converted to Integer" error.
If no row is selected then GridView1.SelectedIndex is -1. You might have to add a check
If GridView1.SelectedIndex <> -1 Then
' Use GridView1.SelectedIndex here
End If
However, it is easier to access the selected row like this:
Dim row = GridView1.SelectedRow
If Not row Is Nothing AndAlso IsNumeric(row.Cells(2).Text) Then
Dim SocioNumInfo As Integer = CInt(row.Cells(2).Text)
....
End If
Note that the cell index is zero based. row.Cells(4) is in the 5th column.
Related
i had created a text box control array using the following code, where the no.of controls are generated on the basics of the value selected from the combo box
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim lstTextBox = New List(Of TextBox)()
For i As Integer = 0 To DropDownList1.SelectedValue
for j as integer =0 to 3
Dim txtbx As New TextBox()
txtbx.ID = String.Format("txtbx{0}", j)
lstTextBox.Add(txtbx)
txtbx.Text = "sample text " & j
form1.Controls.Add(txtbx)
next
Next
Session("lstTextBox") = lstTextBox
End Sub
now i need to insert the value to the database on a single button click, here the code is
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
dim mysql as string
mysql="insert into mytable values("
Dim lstText = New List(Of TextBox)()
lstText = Session("lstTextBox")
For i As Integer = 0 To lstText.Count - 1
for j as integer =0 to 3
mysql= mysql & "'" & trim(lstText(j).Text) & "',"
next
'execute insert for each row with the mysql string
Next
End Sub
now the problem is: only the initial values in the text boxes are inserted to the database, the changes which i had made in the text boxes are not inserted to the database. ie, in session the initial values will not change on text change in the text box.
i need a solution for this problem for proceeding my work. so expert's kind attention please.. positive responses will be appreciated.
Am not sure about VB and as well as My Sql. But i can give you a some way to get rid off from Session
Try like this in DropDownList1_SelectedIndexChanged,
if(Session("lstTextBox"))==null
Session("lstTextBox") = lstTextBox
else
Session.Abandon()
Session("lstTextBox") = lstTextBox
Now you go can check whether session is updating or not. As well as you may get the result as you want. Please let me know the output.
I have the below code which is triggering a stored procedure to delete a record by matching the parameter with the primary key in the table. I ran the code through debug ang everything is assigning properly, but I keep receiving an error stating there are too many arguments specified. I think it has something to do with my stored procedure because the values are being assigned properly.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim CurrentCommand As String = e.CommandName
Dim CurrentRow As Integer = Int32.Parse(e.CommandArgument.ToString())
Dim ID As String = GridView1.DataKeys(CurrentRow).Value
sqlds.DeleteParameters.Add("KeyOpType", ID)
sqlds.Delete()
End Sub
Stored Procedure in SQL Server 2008
ALTER PROCEDURE [dbo].[GetGenInfo_DeleteMines]
(#KeyOpType int)
AS
Delete FROM GenInfo_OpType
Where KeyOpType = #KeyOpType
Where are you setting your delete command? Do we know for sure that it is calling the correct procedure? I think this is what you're trying to do . Check this:
SQLDataSource with GridView and CRUD
Not sure if this will fix the problem what your having.... but
It appears you are missing the BEGIN in the stored procedure. I'm new to this, but i think that is required.
ALTER PROCEDURE [dbo].[GetGenInfo_DeleteMines]
(#KeyOpType int)
AS
BEGIN <-----------------------------------MISSING THIS
Delete FROM GenInfo_OpType
Where KeyOpType = #KeyOpType
Plus, i don't see where you are calling the SP in the vb code.
EDIT: To give further examples....
My code behind on the gv Row Command would look like this... (if its coming from a button/link on the gv have to add CommandName="SomeCommandName" to the control on the gv)
Protected Sub gvName_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvName.RowCommand
Dim tGrid As GridView = DirectCast(sender, GridView)
Dim tIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = Nothing
Select Case e.CommandName
Case "SomeCommandName"
CallToDelete(tGrid.DataKeys(tIndex).Value)
End Select
End Sub
I have a GridView displaying results from sql server, all values are supposed to follow on the previous value e.g. 3373 must be before 3372. I need to colour the row on the gridview as soon as the value does not follow the previous value. Sometimes the values are missing so I need to identify if a value is missing or not.
You could use the OnRowDataBound event to store the last value and compare. Something like this:
Private _lastRowValue As Integer = -1
Protected Sub OnGridViewRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If _lastRowValue <> -1 Then
' read current row value, compare and then format, e.g. like this
e.Row.Cells(1).Text = "<i>" + e.Row.Cells(1).Text + "</i>"
End If
_lastRowValue = ... ' read value from cell
End If
End Sub
I have an unbound Gridview that is populated by a Linq to Entities query and would like to convert string values in a particular column to lowercase.
In the Gridview's RowDataBound event, i have tried StrConv(e.Row.Cells(3).Text, VbStrConv.ProperCase) but this doesn't work.
I have also tried StrConv(emp.Name, VbStrConv.ProperCase) in the LiNQ to Entities query but still the Name values returned are to converted to Lower-case.
Protected Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim cellDate As Date
If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
End If
Next
End If
StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
End Sub
As far as I can see, strConv returns a string, which should be used, I think like:
e.Row.Cells(4).Text = StrConv(e.Row.Cells(4).Text, VbStrConv.ProperCase)
have you tried to do this:
string strLower = e.Row.Cells[0].Text.ToLower();
and then use the strLower as the lower case string.
I am trying to loop through the rows of my gridview and retrieve the data key value of each row and then execute some code to run sql queries. How can I get the data key value of each row in variable? right now I am receiving an error message saying:
value of type system.web.ui.webcontrols.datakey cannot be converted to integer.
Here is my code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each row As GridViewRow In GridView1.Rows
Dim therowindex As Integer = row.RowIndex
Dim theid As Integer = GridView1.DataKeys([therowindex])
'execute some more code running queries using the data key value.
Next
End Sub
You can iterate through the .Rows collection, and then find the row's DataKey value.
foreach(GridViewRow row in GridView1.Rows)
{
string theDataKey = GridView1.DataKeys[row.RowIndex].Value.ToString();
// Do something with your index/key value
}
You have to use Value property.
Dim theid As Integer = Integer.Parse(GridView1.DataKeys(therowindex).Value.ToString())