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.
Related
I have a gridview in which I insert a checkbox on check_changed even I write the following codes to get the value of Issue Id as mentioned in image also showing values in textbox, but when I use these string in SQL:
Select * from IssueBook Where IssueId IN (values)
It shows error converting varchar to numeric on check_changed I write these codes I have take IssueId (numeric)
Protected Sub CheckBox1_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
Dim x As String = ""
For Each row As GridViewRow In GridView1.Rows
Dim cb As CheckBox = row.FindControl("checkbox1")
If cb IsNot Nothing AndAlso cb.Checked Then
If x <> "" Then
x += ","
End If
x += row.Cells(1).Text
End If
Next
RwId.Text = x
Session("SelctdIsuedBokNo") = RwId.Text
In data table I used numeric value.
How can I convert above codes into integers such as 4,5,6, with comma separator?
The IN statement requires a list of comma separated values, not a "string" with those values.
You should create a dynamic SQL and pass it to your Command (SqlCommand or whatever method you are using to execute SQL)... something like: (If you are using SqlClient)
SqlCommand cmd = new SqlCommand(String.Format("Select * from IssueBook Where IssueId IN ({0})", values), your_connection);
Hope it helps.
How about use Convert.ToInt32(); function
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
you can use an array :
http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx
and convert values into Integer with
Integer.TryParse
http://www.dotnetperls.com/array-vbnet
Try to use
Integer.Parse()
or
Integer.TryParse()
Hope it works.
You can't convert the string "4,5,6" into a single integer value.
Your best bet is to change the select to accept a varchar value.
In this case to avoid sql injection and as long as your IDs (Cells(1).Text values) are integers you should collect your IDs from cells and type safe them in an integer list, then join them with a comma into a string at the end
I've adjusted your code accordingly.
Protected Sub CheckBox1_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Values As List(Of Integer)'your integer list of IDs
For Each row As GridViewRow In GridView1.Rows
Dim cb As CheckBox = row.FindControl("checkbox1")
If cb IsNot Nothing AndAlso cb.Checked Then
Values.Add(Integer.convert(row.Cells(1).Text))'add the ID
End If
Next
RwId.Text = String.Join(",", Values)'pull out the list into a comma delimited string eg "2,45,67,87"
Session("SelctdIsuedBokNo") = RwId.Text
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 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.
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 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())