Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my gv_RowCommand code behind. The problem is when I click button "cmdSelect" in different row,it will display the same information. All my data, the status is '2'. Updated code:


VB
Protected Sub gvCheckList_RowCommand(sender As Object, e As GridViewCommandEventArgs)
     Try
        If e.CommandName = "cmdSelect" Then
            Dim index As Integer = Int32.Parse(e.CommandArgument.ToString())
            Dim row As GridViewRow = gvCheckList.Rows(index)
            Dim MatricNo As String = gvCheckList.DataKeys(index).Value

            sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
                & "c.Officer " _
                & "FROM KaunselingPL AS a " _
                & "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
                & "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
                & "WHERE (a.Status = '2' AND a.MatricNo = '" & MatricNo &"')"

            dbconn = New OleDbConnection(strCon)
            dbconn.Open()

            dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()
            If (dbread.HasRows()) Then
               Me.lblName.Text = dbread("Name")
               Me.lblMatricNo.Text = dbread("MatricNo")
               Me.lblFaculty.Text = dbread("Faculty")
               Me.lblTelNo.Text = dbread("TelNo")
               Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
               Me.lblOfficer.Text = dbread("Officer")
            End If
            dbread.Close()
            dbcomm = Nothing
            dbconn.Close()
        End If
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
     End Try
    End Sub


ASP.NET
<asp:gridView id="gvCheckList"  CssClass="gridViewTable" autogeneratecolumns="False" allowpaging="True"  Width="100%"  EmptyDataText="No Recordd" runat="server" PageSize="20" OnRowCommand="gvCheckList_RowCommand">
                               
                        <FooterStyle CssClass="gridViewFooter" />
                        <HeaderStyle CssClass="gridViewHeader" />
                        <AlternatingRowStyle CssClass="gridViewAlternating" />

                        <Columns> 
                            <asp:BoundField DataField="ROWNUMBER" HeaderText="No." HeaderStyle-Width="2%"></asp:BoundField>
                            <asp:ButtonField DataTextField="MatricNo" CommandName="cmdSelect" HeaderText="Matric No" meta:resourcekey="ButtonFieldResource1"  HeaderStyle-Width="15%" ></asp:Butt>
                            <asp:ButtonField DataTextField="Name" HeaderText="Name" HeaderStyle-Width="32%"></asp:Butt>
                            <asp:BoundField DataField="Faculty" HeaderText="Fakulti" HeaderStyle-Width="25%"></asp:BoundField>
                            <asp:BoundField DataField="Date" HeaderText="Date" HeaderStyle-Width="25%" DataFormatString="{0:dd/MM/yyyy}"></asp:BoundField> 

                            <asp:BoundField DataField="KaunselingID" HeaderText="Hidden">
                                <ItemStyle CssClass="hidden" VerticalAlign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <FooterStyle CssClass="hidden" />
                            </asp:BoundField> 
                        </Columns>

                        <FooterStyle CssClass="gridViewFooter" />
                        <HeaderStyle ForeColor="White" CssClass="gridViewHeader" />
                        
                        <PagerSettings FirstPageText="<<" LastPageText=">>" Mode="NextPreviousFirstLast" NextPageText=">" PreviousPageText="<" />
                   </asp:gridView>
Posted
Updated 24-Sep-14 14:25pm
v6
Comments
virang_21 21-Sep-14 21:29pm    
Your sql statement is not using any value from row. All I can see from where condition is status =2 which will give you same information all the time.
U_Hanisa 21-Sep-14 22:07pm    
So what would I do to display all list data in grid view, then when I click button 'Select' on certain row data, it will display that information. Did you get what I mean.
ChauhanAjay 21-Sep-14 23:38pm    
You mean to say that if you have a "Select" button on row1 then when you click on the select button it should display the data related to that row "row1". Kindly let me know if I have understood your problem in a correct manner, if not kindly correct me.
U_Hanisa 21-Sep-14 23:46pm    
Yes, you right. And when I click select button on row2, it will display data related to row2.
ChauhanAjay 21-Sep-14 23:53pm    
Try changing your query from

sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
& "c.Officer " _
& "FROM KaunselingPL AS a " _
& "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
& "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
& "WHERE (a.Status = '2')"

To

sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
& "c.Officer " _
& "FROM KaunselingPL AS a " _
& "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
& "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
& "WHERE (a.Status = '2' and a.MatricNo='"& index.ToString() &"')"

Try the above query and let me know the result.

In the query you will need to change the row.Cells(0).Text to match the column index of the column "Matric No" and then try.
VB
Protected Sub gvCheckList_RowCommand(sender As Object, e As GridViewCommandEventArgs)
	Try
		If e.CommandName = "cmdSelect" Then
			Dim index As Integer = Int32.Parse(e.CommandArgument.ToString())
			Dim row As GridViewRow = gvCheckList.Rows(index)
Dim matricNo As String = row.Cells(1).Text.ToString()
Response.Write("Matric No = "&matricNo)
			sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
			& "c.Officer " _
			& "FROM KaunselingPL AS a " _
			& "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
			& "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
			& "WHERE (a.Status = '2' and a.MatricNo='"& matricNo &"')"
Response.Write("Sql Query = "&sql)		 
			dbconn = New OleDbConnection(strCon)
			dbconn.Open()
		 
			dbcomm = New OleDbCommand(sql, dbconn)
			dbread = dbcomm.ExecuteReader()
			dbread.Read()
			If (dbread.HasRows()) then 
				Me.lblName.Text = dbread("Name")
				Me.lblMatricNo.Text = dbread("MatricNo")
				Me.lblFaculty.Text = dbread("Faculty")
				Me.lblTelNo.Text = dbread("TelNo")
				Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
				Me.lblOfficer.Text = dbread("Officer")
			End If 
			dbread.Close()
			dbcomm = Nothing
			dbconn.Close()
		End If
	Catch ex As Exception
		Response.Write(ex.Message.ToString());
	End Try
End Sub
 
Share this answer
 
v4
Comments
U_Hanisa 22-Sep-14 22:08pm    
The writing becomes larger
ChauhanAjay 22-Sep-14 22:09pm    
I dont understand what you are trying to say
U_Hanisa 22-Sep-14 22:16pm    
When I click the select button on Matric No, the writing in gridview becomes larger
ChauhanAjay 22-Sep-14 22:32pm    
What about the result are you able to get the correct result.
I am not sure why the writing becomes larger.
U_Hanisa 22-Sep-14 22:35pm    
No. When I click select button in gridview, it don't display any data.
VB
If e.CommandName = "cmdSelect" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gvCheckList.Rows(index)

            Me.hidIDKaunseling.Value = Convert.ToString(row.Cells(5).Text)
            Me.lblMatricNo.Text = Convert.ToString(row.Cells(6).Text)

            '---LOAD SENARAI---
            dbconn = New OleDb.OleDbConnection(strCon)
            dbconn.Open()

            sql = "SELECT a.MatricNo, b.Name, b.Faculty, a.Date, a.KaunselingID, b.TelNo, " _
                & "c.Officer " _
                & "FROM KaunselingPL AS a " _
                & "INNER JOIN DbStudent..Personal AS b ON a.MatricNo = b.MatricNo " _
                & "INNER JOIN KaunselingDT AS c ON a.KaunselingID = c.KaunselingID " _
                & "WHERE (a.Status = '2' and a.MatricNo='" & Me.lblMatricNo.Text & "'"

            dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()

            Me.lblName.Text = dbread("Name")
            Me.lblMatricNo.Text = dbread("MatricNo")
            Me.lblFaculty.Text = dbread("Faculty")
            Me.lblTelNo.Text = dbread("TelNo")
            Me.lblDate.Text = CDate(dbread("Date")).ToString("dd/MM/yyyy")
            Me.lblOfficer.Text = dbread("Officer")

            dbread.Close()
            dbcomm = Nothing
            dbconn.Close()
        End If


ASP.NET
<asp:gridview id="gvCheckList" cssclass="gridViewTable" autogeneratecolumns="False" allowpaging="True" width="100%" emptydatatext="No Record" runat="server" pagesize="20" onrowcommand="gvCheckList_RowCommand" xmlns:asp="#unknown">
                               
                        <footerstyle cssclass="gridViewFooter" />
                        <HeaderStyle CssClass="gridViewHeader" />
                        <alternatingrowstyle cssclass="gridViewAlternating" />

                        <columns> 
                            <asp:boundfield datafield="ROWNUMBER" headertext="No." headerstyle-width="2%"></asp:boundfield>
                            <asp:buttonfield datatextfield="MatricNo" commandname="cmdSelect" headertext="Matric No." meta:resourcekey="ButtonFieldResource1" headerstyle-width="15%" xmlns:meta="#unknown"></asp:buttonfield>
                            <asp:boundfield datafield="Name" headertext="Student's Name" headerstyle-width="35%"></asp:boundfield>
                            <asp:boundfield datafield="Faculty" headertext="Fakulti" headerstyle-width="25%"></asp:boundfield>
                            <asp:boundfield datafield="Date" headertext="Date" headerstyle-width="25%" dataformatstring="{0:dd/MM/yyyy}"></asp:boundfield> 

                            <asp:boundfield datafield="KaunselingID" headertext="Hidden">
                                <itemstyle cssclass="hidden" verticalalign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <footerstyle cssclass="hidden" />
                            </asp:boundfield>
                           
                            <asp:boundfield datafield="Matric No." headertext="Hidden">
                                <itemstyle cssclass="hidden" verticalalign="Top" />
                                <HeaderStyle CssClass="hidden" />
                                <footerstyle cssclass="hidden" />
                            </asp:boundfield>
                             
                        </columns>

                        <footerstyle cssclass="gridViewFooter" />
                        <HeaderStyle ForeColor="White" CssClass="gridViewHeader" />
                        <pagersettings firstpagetext="<<" lastpagetext=">>" mode="NextPreviousFirstLast" nextpagetext=">" previouspagetext="<" />

                   </asp:gridview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900