Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > EDIT ข้อมูลใน Gridview แล้ว แสดงผลผิดพลาด ไม่ตรงกับ Index เลือกไว้



 

EDIT ข้อมูลใน Gridview แล้ว แสดงผลผิดพลาด ไม่ตรงกับ Index เลือกไว้

 



Topic : 060098



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์




รูปบแรก

รูปที่ 1 ก็แสดง reocrd ทั้งหมดออกมาไม่มีอะไรครับ

รูป 2

รูปที่ 2 ผมจะทำการ search ข้อมูลตามรูป และ เลือก EDIT ใน ลำดับที่ 53

รป 3

รูปที่ 3 หลังจากกด EDIT ลำดับที่ 53 มันดันแสดง record ทั้งหมด แถมไป EDIT ลำดับ 48 แทน (ซึ่งเป็น index = 3 นับจาก 0)

รุป 4

รูปที่ 4 พอ ค้นหาอีกครั้ง จะแสดงข้อมูลที่ถูกต้อง

พอทราบไหมว่าเกิดจากอะไรครับ



อันนี้ code grdview กับ datasource ที่ใช้ในการติดต่อ

Code (ASP)
<table style="width: 1024px;">
        <tr>
            <td>
                <asp:RadioButton ID="radId" runat="server" GroupName="Search" Text="ลำดับที่" />
                <asp:RadioButton ID="radFis" runat="server" GroupName="Search" 
                    Text="ปีงบประมาณ" />
                <asp:RadioButton ID="radPro" runat="server" GroupName="Search" 
                    Text="หัวข้อโครงการ" />
                <asp:RadioButton ID="radRes" runat="server" GroupName="Search" 
                    Text="ชื่อผู้วิจัย/ผู้ร่วมวิจัย" />
                <asp:RadioButton ID="radFac" runat="server" GroupName="Search" Text="คณะ" />
                <asp:RadioButton ID="radBud" runat="server" GroupName="Search" 
                    Text="งบประมาณ" />
                <asp:RadioButton ID="radSou" runat="server" GroupName="Search" 
                    Text="แหล่งทุน" />
                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
                <asp:Button ID="btnSearch" runat="server" Text="ค้นหา" />
            </td>
        </tr>
    </table>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" 
            DataKeyNames="Id" DataSourceID="SqlDataSource1" 
            GridLines="None" BackColor="White" 
            BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="ลำดับที่" InsertVisible="False" 
                    ReadOnly="True" SortExpression="Id" >
                <HeaderStyle Font-Bold="True" ForeColor="White" Wrap="False" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="ปีงบประมาณ" SortExpression="FiscalYear">
                    <EditItemTemplate>
                        <asp:DropDownList ID="addFiscalYear" runat="server" 
                            ControlToValidate="addFiscalYear" SelectedValue='<%# Bind("FiscalYear") %>' 
                            Width="60px">
                            <asp:ListItem>2545</asp:ListItem>
                            <asp:ListItem>2546</asp:ListItem>
                            <asp:ListItem>2547</asp:ListItem>
                            <asp:ListItem>2548</asp:ListItem>
                            <asp:ListItem>2549</asp:ListItem>
                            <asp:ListItem>2550</asp:ListItem>
                            <asp:ListItem>2551</asp:ListItem>
                            <asp:ListItem>2552</asp:ListItem>
                            <asp:ListItem>2553</asp:ListItem>
                            <asp:ListItem>2554</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("FiscalYear") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="White" Wrap="False" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:BoundField DataField="ProjectName" HeaderText="หัวข้อโครงการ" 
                    SortExpression="ProjectName" >
                <HeaderStyle ForeColor="White" Wrap="False" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:BoundField DataField="ResearchersName" HeaderText="ชื่อผู้วิจัย/ผู้ร่วมวิจัย" 
                    SortExpression="ResearchersName" >
                <HeaderStyle ForeColor="White" Wrap="False" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="คณะ" SortExpression="Faculty">
                    <EditItemTemplate>
                        <asp:DropDownList ID="addFaculty" runat="server" 
                            SelectedValue='<%# Bind("Faculty") %>' Width="150px">
                            <asp:ListItem Value="00">เลือกคณะ</asp:ListItem>
                            <asp:ListItem>เกษตรศาสตร์ทรัพยากรธรรมชาติและสิ่งแวดล้อม</asp:ListItem>
                            <asp:ListItem>วิทยาศาสตร์</asp:ListItem>
                            <asp:ListItem>วิทยาลัยพลังงานและสิ่งแวดล้อม</asp:ListItem>
                            <asp:ListItem>เทคโนโลยีสารสนเทศและการสื่อสาร</asp:ListItem>
                            <asp:ListItem>ศิลปศาสตร์</asp:ListItem>
                            <asp:ListItem>วิทยาการจัดการและสารสนเทศศาสตร์</asp:ListItem>
                            <asp:ListItem>วิศวกรรมศาสตร์</asp:ListItem>
                            <asp:ListItem>วิทยาศาสตร์การแพทย์ </asp:ListItem>
                            <asp:ListItem>ส่วนงานบริหารกลาง</asp:ListItem>
                            <asp:ListItem>ส่วนงานกิจการนิสิต</asp:ListItem>
                            <asp:ListItem>IT</asp:ListItem>
                            <asp:ListItem>พยาบาลศาสตร์</asp:ListItem>
                            <asp:ListItem>นิติศาสตร์</asp:ListItem>
                            <asp:ListItem>เภสัชศาสตร์</asp:ListItem>
                            <asp:ListItem>วิทยาศาสตร์</asp:ListItem>
                            <asp:ListItem>วิทยาการจัดการ</asp:ListItem>
                            <asp:ListItem>สหเวชศาสตร์</asp:ListItem>
                            <asp:ListItem>แพทยศาสตร์</asp:ListItem>
                            <asp:ListItem>หน่วยปฏิบัติการชีวโมเลกุล</asp:ListItem>
                            <asp:ListItem>อื่นๆ</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Faculty") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="White" Wrap="False" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:BoundField DataField="Budget" HeaderText="งบประมาณ" 
                    SortExpression="Budget" >
                <HeaderStyle ForeColor="White" Wrap="False" />
                <ItemStyle HorizontalAlign="Center" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="แหล่งทุน" SortExpression="SourceFunds">
                    <EditItemTemplate>
                        <asp:DropDownList ID="addSourceFunds" runat="server" Height="22px" 
                            SelectedValue='<%# bind("SourceFunds") %>' Width="170px">
                            <asp:ListItem Value="000">เลือกแหล่งทุน</asp:ListItem>
                            <asp:ListItem>งบประมาณรายได้มหาวิทยาลัย</asp:ListItem>
                            <asp:ListItem>งบประมาณแผ่นดิน</asp:ListItem>
                            <asp:ListItem>สำนักงานกองทุนสนับสนุนการสร้างเสริมสุขภาพ (สสส.)</asp:ListItem>
                            <asp:ListItem>โครงการความร่วมมือระหว่างสำนักงานคณะกรรมการอุดมศึกษากับสำนักงานกองทุนสนับสนุนการวิจัย</asp:ListItem>
                            <asp:ListItem>สำนักงานปฏิรูปที่ดินเพื่อการเกษตร (สปก.)</asp:ListItem>
                            <asp:ListItem>สำนักงานคณะกรรมการวัฒนธรรมแห่งชาติ</asp:ListItem>
                            <asp:ListItem>บริษัท มั่งมีศรีสุข จำกัด</asp:ListItem>
                            <asp:ListItem>ภาคเหนือตอนล่าง</asp:ListItem>
                            <asp:ListItem>โครงการคลินิคเทคโนโลยี</asp:ListItem>
                            <asp:ListItem>สำนักงานคณะกรรมการข้อมูลข่าวสารของราชการ สำนักงานปลัดสำนักนายกรัฐมนตรี</asp:ListItem>
                            <asp:ListItem>งานตีพิมพ์เผยแพร่</asp:ListItem>
                            <asp:ListItem>สกอ+สกว ทุนพัฒนาศักยภาพในการทำงานวิจัยของอาจารย์รุ่นใหม่โครงการความร่วมมือระหว่างสำนักงานคณะกรรมการการอุดมศึกษา (สกอ.) กับสำนักงานกองทุนสนับสนุนการวิจัย (สกว.) ประจำปี 2551</asp:ListItem>
                            <asp:ListItem>สำนักงานกองทุนสนับสนุนการวิจัยทุนวิจัยมหาบัณฑิต สกว.(สกว.-สสว)</asp:ListItem>
                            <asp:ListItem>สำนักงานคณะกรรมการวิจัยแห่งชาติ(วช.)</asp:ListItem>
                            <asp:ListItem>สำนักงานกองทุนสนับสนุนการวิจัย (สกว)</asp:ListItem>
                            <asp:ListItem>สำนักงานเทศบาลเมืองพะเยา</asp:ListItem>
                            <asp:ListItem>กองทุนเงินนอกงบประมาณ มหาวิทยาลัยนเรศวร พะเยา</asp:ListItem>
                            <asp:ListItem>กองทุนสนับสนุนการวิจัย(สกว.)</asp:ListItem>
                            <asp:ListItem>ทุนการวิจัย พัฒนาและวิศวกรรม ศูนย์เทคโนโลยีโลหะและวัสดุแห่งชาติ สำนักงานพัฒนาวิทยาศาสตร์และเทคโนโลยีแห่งชาติ</asp:ListItem>
                            <asp:ListItem>ทุนเครือข่ายการวิจัยภาคเหนือตอนล่าง</asp:ListItem>
                            <asp:ListItem>สำนักงานปลัดสำนักนายกรัฐมนตรี โดยสำนักงานคณะกรรมการข้อมูลข่าวสาร</asp:ListItem>
                            <asp:ListItem>สถาบันไฟฟ้าและอิเล็กทรอนิกส์(อุตสาหกรรมพัฒนามูลนิธิ)</asp:ListItem>
                            <asp:ListItem>สถาบันเพื่อการบริหารทรัพยากรธรรมชาติและสิ่งแวดล้อม</asp:ListItem>
                            <asp:ListItem>สำนักงานปลัดกระทรวงทรัพยากรธรรมชาติและสิ่งแวดล้อม</asp:ListItem>
                            <asp:ListItem>การกีฬาแห่งประเทศไทย (กกท.)</asp:ListItem>
                            <asp:ListItem>IRPUS สกว.</asp:ListItem>
                            <asp:ListItem>โครงการเครือข่ายสวทช. ภาคเหนือ</asp:ListItem>
                            <asp:ListItem>องค์การบริหารส่วนตำบล 44 ที่</asp:ListItem>
                            <asp:ListItem>ทุนวิจัยพัฒนาและวิศวกรรม โครงการเครือข่าย สวทช. ภาคเหนือ</asp:ListItem>
                            <asp:ListItem>สถาบันวิจัยวิทยาศาสตร์และเทคโนโลยีแห่งประเทศไทย(วว.)</asp:ListItem>
                            <asp:ListItem>โครงการให้ทุนสนับสนุนโครงงานอุตสาหกรรมสำหรับนักศึกษาปริญญาตรี (IRPUS) สกว.</asp:ListItem>
                            <asp:ListItem>องค์การกระจายเสียงและแพร่ภาพสาธารณะแห่งประเทศไทย(สสท)</asp:ListItem>
                            <asp:ListItem>สถาบันคุ้มครองผู้บริโภคในกิจการโทรคมนาคม สนง.คณะกรรมการกิจการโทรคมนาคมแห่งชาติ</asp:ListItem>
                            <asp:ListItem>ศูนย์เทคโนโลยีโลหะและวัสดุแห่งชาติ(MTEC)</asp:ListItem>
                            <asp:ListItem>กระทรวงวิทยาศาสตร์และเทคโนโลยี</asp:ListItem>
                            <asp:ListItem>ศูนย์วิจัยร่วมเฉพาะทางด้านส่วนประกอบฮาร์ดดิสก์ไดรฟ์ (I/UCRC in HDD  Component) คณะวิศวกรรมศาสตร์ ม.ขอนแก่น</asp:ListItem>
                            <asp:ListItem>สนง.นโยบายและแผนพลังงาน กระทรวงพลังงาน</asp:ListItem>
                            <asp:ListItem>สำนักงานนโยบายและแผนพลังงาน(สนพ.)</asp:ListItem>
                            <asp:ListItem>สำนักงานทรัพยากรธรรมชาติสิ่งแวดล้อม</asp:ListItem>
                            <asp:ListItem>โครงการส่งเสริมการวิจัยสำนักงานคณะกรรมการการอุดมศึกษา</asp:ListItem>
                            <asp:ListItem>สำนักงานคณะกรรมการการอุดมศึกษา (สกอ.)</asp:ListItem>
                            <asp:ListItem>ศูนย์ส่งเสริมอุตสาหกรรมภาค 1</asp:ListItem>
                            <asp:ListItem>สำนักงานพัฒนาวิทยาศาสตร์และเทคโนโลยีแห่งชาติ</asp:ListItem>
                            <asp:ListItem>ศูนย์วิจัยและฝึกอบรมนิเวศวิทยาอุตสาหกรรม</asp:ListItem>
                            <asp:ListItem>อุทยานวิทยาศาสตร์ภาคเหนือ(อวน.)สถาบันวิจัยวิทยาศาสตร์และเทคโนโลยีแห่งประเทศไทย (วว.)</asp:ListItem>
                            <asp:ListItem>สำนักงานพัฒนาวิทยาศาสตร์และเทคโนโลยีแห่งชาติ (สวทช.)</asp:ListItem>
                            <asp:ListItem>อื่นๆ</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("SourceFunds") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="White" Wrap="False" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#594B9C" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#33276A" />
        </asp:GridView>


Code (ASP)
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:researchDBConnectionString %>" 
            DeleteCommand="DELETE FROM [researchs] WHERE [Id] = @Id" 
            InsertCommand="INSERT INTO [researchs] ([FiscalYear], [ProjectName], [ResearchersName], [Faculty], [Budget], [SourceFunds]) VALUES (@FiscalYear, @ProjectName, @ResearchersName, @Faculty, @Budget, @SourceFunds)" 
            SelectCommand="SELECT * FROM [researchs]" 
            UpdateCommand="UPDATE [researchs] SET [FiscalYear] = @FiscalYear, [ProjectName] = @ProjectName, [ResearchersName] = @ResearchersName, [Faculty] = @Faculty, [Budget] = @Budget, [SourceFunds] = @SourceFunds WHERE [Id] = @Id">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="FiscalYear" Type="Int16" />
                <asp:Parameter Name="ProjectName" Type="String" />
                <asp:Parameter Name="ResearchersName" Type="String" />
                <asp:Parameter Name="Faculty" Type="String" />
                <asp:Parameter Name="Budget" Type="Int32" />
                <asp:Parameter Name="SourceFunds" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="FiscalYear" Type="Int16" />
                <asp:Parameter Name="ProjectName" Type="String" />
                <asp:Parameter Name="ResearchersName" Type="String" />
                <asp:Parameter Name="Faculty" Type="String" />
                <asp:Parameter Name="Budget" Type="Int32" />
                <asp:Parameter Name="SourceFunds" Type="String" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>


ส่วนนี้ code ของปุ่ม ค้นหา
Code (VB.NET)
 Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        If radId.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where Id like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radFis.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where FiscalYear like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radPro.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where ProjectName like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radRes.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where ResearchersName like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radFac.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where Faculty like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radBud.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where Budget like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If

        If radSou.Checked = True Then
            SqlDataSource1.SelectCommand = "select * from researchs where SourceFunds like '" & txtSearch.Text & "%'"
            SqlDataSource1.DataBind()
        End If




Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET









ประวัติการแก้ไข
2011-05-12 10:26:44
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-05-12 10:23:52 By : mamme View : 1339 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ผมไม่ค่อยชอบวิธีการเขียนแบบนี้ซะเท่าไหร่ เพระา Debug หาข้อผิดพลาดยากครับ ลองเขียนเป็นแบบ Code Behide น่ะครับ

Go to : ASP.NET GridView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-05-12 12:19:12 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : EDIT ข้อมูลใน Gridview แล้ว แสดงผลผิดพลาด ไม่ตรงกับ Index เลือกไว้
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่