raju1
asked on
how to retrieve value of a particular cell of datagrid
Dear Experts,
I want to retrieve cell value into a variable or text box. Pls help.
I want to retrieve cell value into a variable or text box. Pls help.
ASKER
Thanks for reply. I could not solve this problem. I have written the code in where i have got the errors. Pls note that the datagrid is user_info.
Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim strSQL As String, strConnection As String
Dim EditText As TextBox, iRow As Integer
strConnection = ConfigurationSettings.AppS ettings("c onnections tring")
Dim myCon As New SqlConnection(strConnectio n)
myCon.Open()
iRow = E.Item.ItemIndex
EditText = user_info.Items[irow].ce ' is not working
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0])
Dim cmd As New SqlCommand(strSQL, myCon)
cmd.ExecuteNonQuery()
LoadMyCalendarData()
End Sub
Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim strSQL As String, strConnection As String
Dim EditText As TextBox, iRow As Integer
strConnection = ConfigurationSettings.AppS
Dim myCon As New SqlConnection(strConnectio
myCon.Open()
iRow = E.Item.ItemIndex
EditText = user_info.Items[irow].ce ' is not working
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0])
Dim cmd As New SqlCommand(strSQL, myCon)
cmd.ExecuteNonQuery()
LoadMyCalendarData()
End Sub
try this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].Text)
are there controls in the first column or it is bound column that is not visible to user ?
B..M
mmarinov
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].Text)
are there controls in the first column or it is bound column that is not visible to user ?
B..M
mmarinov
ASKER
Yes, it is first column. I have done according to ur comment, but it is not working. It generates the following error message.
Value of type 'System.Web.UI.WebControls .TableCell Collection ' cannot be converted to 'String'.
Value of type 'System.Web.UI.WebControls
raju1,
use the code from my second post
you can direct cast the e.item.Cells(index)
aslo i've noticed that there is an error
use this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).Text)
Regards!
B..M
mmarinov
use the code from my second post
you can direct cast the e.item.Cells(index)
aslo i've noticed that there is an error
use this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).Text)
Regards!
B..M
mmarinov
ASKER
Thanks for quick response.
yeah i have used as follows:
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].text)
It generates the following error:
Value of type 'System.Web.UI.WebControls .TableCell Collection ' cannot be converted to 'String'.
yeah i have used as follows:
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells[0].text)
It generates the following error:
Value of type 'System.Web.UI.WebControls
raju1,
not this, but this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).text) <-- see here there are no [] brakets
Regards!
B..M
mmarinov
not this, but this
strSQL = "DELETE UserInfo WHERE UserID = " & cstr(E.Item.Cells(0).text)
Regards!
B..M
mmarinov
ASKER
Now the coding is as follows:
Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim strSQL As String, strConnection As String
Dim EditText As TextBox, iRow As Integer
strConnection = ConfigurationSettings.AppS ettings("c onnections tring")
Dim myCon As New SqlConnection(strConnectio n)
myCon.Open()
'iRow = E.Item.ItemIndex
'EditText = user_info.Items[irow].ce
strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)
Dim cmd As New SqlCommand(strSQL, myCon)
cmd.ExecuteNonQuery()
LoadMyCalendarData()
End Sub
At this moment, after debugging the strSQL contains the following value:
"DELETE UserInfo WHERE UserID = "
i.e., CStr(E.Item.Cells(0).Text) can not contain value.
For that here is new error like
Incorrect syntax near '='.
Sub DEDR_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
Dim strSQL As String, strConnection As String
Dim EditText As TextBox, iRow As Integer
strConnection = ConfigurationSettings.AppS
Dim myCon As New SqlConnection(strConnectio
myCon.Open()
'iRow = E.Item.ItemIndex
'EditText = user_info.Items[irow].ce
strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)
Dim cmd As New SqlCommand(strSQL, myCon)
cmd.ExecuteNonQuery()
LoadMyCalendarData()
End Sub
At this moment, after debugging the strSQL contains the following value:
"DELETE UserInfo WHERE UserID = "
i.e., CStr(E.Item.Cells(0).Text)
For that here is new error like
Incorrect syntax near '='.
raju1,
can you post the datagrid html
if the e.item.cells(0).text does not return anything, you have 2 scenarios:
1. there is nothing - you don't bind it
2. you use template column
Regards!
B..M
mmarinov
can you post the datagrid html
if the e.item.cells(0).text does not return anything, you have 2 scenarios:
1. there is nothing - you don't bind it
2. you use template column
Regards!
B..M
mmarinov
ASKER
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="usercode.aspx. vb" Inherits="bismilla.usercod e"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>usercode</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScri pt" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout ">
<FORM id="Form1" runat="server">
<asp:Label id="ErrorMessage" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
runat="server"></asp:Label ><BR>
<asp:LinkButton id="LinkButton1" onclick="DEDR_Add" runat="server" Text="Add new event">Add new user</asp:LinkButton><BR>
<asp:DataGrid id="user_info" runat="server" AutoGenerateColumns="false " width="100%" OnEditCommand="DEDR_Edit"
OnUpdateCommand="DEDR_Upda te" OnCancelCommand="DEDR_Canc el" OnDeleteCommand="DEDR_Dele te">
<HeaderStyle ForeColor="White" BackColor="DodgerBlue" Font-Bold="true" />
<ItemStyle BackColor="White" />
<AlternatingItemStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateColumn HeaderText="User ID">
<ItemTemplate>
<%# DataBinder.Eval(Container. DataItem, "UserID") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserID" Size="10" Text='<%# Container.DataItem("UserID ") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="User Name">
<ItemTemplate>
<%# Container.DataItem("UserNa me") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserName" Size="40" Text='<%# Container.DataItem("UserNa me") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Password">
<ItemTemplate>
<%# Container.DataItem("Passwo rd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtPassword" Size="10" Text='<%# Container.DataItem("Passwo rd") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="User Level">
<ItemTemplate>
<%# Container.DataItem("UserLe vel") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserLevel" Size="7" Text='<%# Container.DataItem("UserLe vel") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Full Name">
<ItemTemplate>
<%# Container.DataItem("FullNa me") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtFullName" Size="40" Text='<%# Container.DataItem("FullNa me") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton CommandName="Edit" Text="Edit" runat="server" />
<asp:LinkButton CommandName="Delete" Text="Delete" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Cancel" Text="Cancel" runat="server" />
<asp:LinkButton CommandName="Update" Text="Update" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></FORM>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>usercode</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScri
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout
<FORM id="Form1" runat="server">
<asp:Label id="ErrorMessage" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"
runat="server"></asp:Label
<asp:LinkButton id="LinkButton1" onclick="DEDR_Add" runat="server" Text="Add new event">Add new user</asp:LinkButton><BR>
<asp:DataGrid id="user_info" runat="server" AutoGenerateColumns="false
OnUpdateCommand="DEDR_Upda
<HeaderStyle ForeColor="White" BackColor="DodgerBlue" Font-Bold="true" />
<ItemStyle BackColor="White" />
<AlternatingItemStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateColumn HeaderText="User ID">
<ItemTemplate>
<%# DataBinder.Eval(Container.
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserID" Size="10" Text='<%# Container.DataItem("UserID
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="User Name">
<ItemTemplate>
<%# Container.DataItem("UserNa
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserName" Size="40" Text='<%# Container.DataItem("UserNa
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Password">
<ItemTemplate>
<%# Container.DataItem("Passwo
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtPassword" Size="10" Text='<%# Container.DataItem("Passwo
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="User Level">
<ItemTemplate>
<%# Container.DataItem("UserLe
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtUserLevel" Size="7" Text='<%# Container.DataItem("UserLe
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Full Name">
<ItemTemplate>
<%# Container.DataItem("FullNa
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtFullName" Size="40" Text='<%# Container.DataItem("FullNa
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton CommandName="Edit" Text="Edit" runat="server" />
<asp:LinkButton CommandName="Delete" Text="Delete" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Cancel" Text="Cancel" runat="server" />
<asp:LinkButton CommandName="Update" Text="Update" runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></FORM>
</body>
</HTML>
raju1,
so replace this line
strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)
with this one
strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl(" txtUserID" ), TextBox).Text
Regards!
B..M
mmarinov
so replace this line
strSQL = "DELETE UserInfo WHERE UserID = " & CStr(E.Item.Cells(0).Text)
with this one
strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl("
Regards!
B..M
mmarinov
ASKER
New error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 123: 'iRow = E.Item.ItemIndex
Line 124: 'EditText = user_info.Items[irow].ce
Line 125: strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl(" txtUserID" ), TextBox).Text
Line 126:
Line 127: Dim cmd As New SqlCommand(strSQL, myCon)
Error in Line: 125
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
Line 123: 'iRow = E.Item.ItemIndex
Line 124: 'EditText = user_info.Items[irow].ce
Line 125: strSQL = "DELETE UserInfo WHERE UserID = " & CType(E.Item.FindControl("
Line 126:
Line 127: Dim cmd As New SqlCommand(strSQL, myCon)
Error in Line: 125
raju1,
debug the program and check this collection before building the query
e.Item.Cells(0).Controls
how many controls are there ? is there any control that is textbox or label if there is any then its Text property will contain the userid value
Regards!
B..M
mmarinov
debug the program and check this collection before building the query
e.Item.Cells(0).Controls
how many controls are there ? is there any control that is textbox or label if there is any then its Text property will contain the userid value
Regards!
B..M
mmarinov
ASKER
E.Item.Cells(0).Controls {System.Web.UI.ControlColl ection} System.Web.UI.ControlColle ction
Count 1 Integer
IsReadOnly False Boolean
IsSynchronized False Boolean
Item <cannot view indexed property> System.Web.UI.Control
Count 1 Integer
IsReadOnly False Boolean
IsSynchronized False Boolean
Item <cannot view indexed property> System.Web.UI.Control
raju1,
so check then what do you have in
e.Item.Cells(0).Controls(0 )
Regards!
B..M
mmarinov
so check then what do you have in
e.Item.Cells(0).Controls(0
Regards!
B..M
mmarinov
ASKER
There are lot of parameters. What information do u need to fix the problem?
- e.Item.Cells(0).Controls(0 ) {System.Web.UI.DataBoundLi teralContr ol} System.Web.UI.Control
- [System.Web.UI.DataBoundLi teralContr ol] {System.Web.UI.DataBoundLi teralContr ol} System.Web.UI.DataBoundLit eralContro l
+ BindingContainer {System.Web.UI.WebControls .DataGridI tem} System.Web.UI.Control
ClientID "user_info__ctl3__ctl2" String
+ Controls {System.Web.UI.EmptyContro lCollectio n} System.Web.UI.ControlColle ction
EnableViewState True Boolean
ID Nothing String
+ NamingContainer {System.Web.UI.WebControls .DataGridI tem} System.Web.UI.Control
+ Page {ASP.usercode_aspx} System.Web.UI.Page
+ Parent {System.Web.UI.WebControls .TableCell } System.Web.UI.Control
Site Nothing System.ComponentModel.ISit e
TemplateSourceDirectory "/bismilla" String
Text "
975
" String
UniqueID "user_info:_ctl3:_ctl2" String
Visible True Boolean
+ BindingContainer {System.Web.UI.WebControls .DataGridI tem} System.Web.UI.Control
ClientID "user_info__ctl3__ctl2" String
- Controls {System.Web.UI.EmptyContro lCollectio n} System.Web.UI.ControlColle ction
+ [System.Web.UI.EmptyContro lCollectio n] {System.Web.UI.EmptyContro lCollectio n} System.Web.UI.EmptyControl Collection
Count 0 Integer
IsReadOnly False Boolean
IsSynchronized False Boolean
Item <cannot view indexed property> System.Web.UI.Control
+ SyncRoot {System.Web.UI.EmptyContro lCollectio n} Object
EnableViewState True Boolean
ID Nothing String
+ NamingContainer {System.Web.UI.WebControls .DataGridI tem} System.Web.UI.Control
+ Page {ASP.usercode_aspx} System.Web.UI.Page
+ Parent {System.Web.UI.WebControls .TableCell } System.Web.UI.Control
Site Nothing System.ComponentModel.ISit e
TemplateSourceDirectory "/bismilla" String
UniqueID "user_info:_ctl3:_ctl2" String
Visible True Boolean
- e.Item.Cells(0).Controls(0
- [System.Web.UI.DataBoundLi
+ BindingContainer {System.Web.UI.WebControls
ClientID "user_info__ctl3__ctl2" String
+ Controls {System.Web.UI.EmptyContro
EnableViewState True Boolean
ID Nothing String
+ NamingContainer {System.Web.UI.WebControls
+ Page {ASP.usercode_aspx} System.Web.UI.Page
+ Parent {System.Web.UI.WebControls
Site Nothing System.ComponentModel.ISit
TemplateSourceDirectory "/bismilla" String
Text "
975
" String
UniqueID "user_info:_ctl3:_ctl2" String
Visible True Boolean
+ BindingContainer {System.Web.UI.WebControls
ClientID "user_info__ctl3__ctl2" String
- Controls {System.Web.UI.EmptyContro
+ [System.Web.UI.EmptyContro
Count 0 Integer
IsReadOnly False Boolean
IsSynchronized False Boolean
Item <cannot view indexed property> System.Web.UI.Control
+ SyncRoot {System.Web.UI.EmptyContro
EnableViewState True Boolean
ID Nothing String
+ NamingContainer {System.Web.UI.WebControls
+ Page {ASP.usercode_aspx} System.Web.UI.Page
+ Parent {System.Web.UI.WebControls
Site Nothing System.ComponentModel.ISit
TemplateSourceDirectory "/bismilla" String
UniqueID "user_info:_ctl3:_ctl2" String
Visible True Boolean
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
datagrid.Items[index of the row].Cells[index of the column].Text <- this will return you the content in the cell if you have not controls there ( bound column for example )
if you have controls
TextBox tb = (TextBox)Datagrid.Items[in
and then tb.Text
Regards!
B..M
mmarinov