更新从下拉列表中指定的数据网格中的特定列(Updating specific columns in a datagrid specified from a dropdown)

编程入门 行业动态 更新时间:2024-10-24 17:32:10
更新从下拉列表中指定的数据网格中的特定列(Updating specific columns in a datagrid specified from a dropdown)

我是ASP.NET和VB的新手:所以我现在处于退出低水平。 我试图使用<asp:BoundField DataField..>或<asp:TemplateField..>显示gridview。 使用Boundfield时,我能够让viewgrid显示我想要的内容,但我无法编辑它。 它告诉我:“输入字符串的格式不正确”。 使用模板时,我甚至无法显示gridview。

如果可以使用gridview我可以工作并能够编辑那个,那么我将坚持这种格式。

我的DB看起来像这样:

[TBL_CASE]( [caseId] [int] IDENTITY(1,1) NOT NULL, [caseSub] [int] NULL, [caseUser] [int] NULL, [caseType] [varchar](200) NULL, [caseRegBy] [varchar](100) NULL, [caseTopic] [varchar](300) NULL, [caseDesc] [varchar](max) NULL, [caseDtCreated] [datetime] NULL, [caseStatus] [bit] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我的GridView是这样的,下面有一个数据源来获取我用来排序显示内容的下拉列表:

<asp:GridView ID="caseList" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowEditing="editCase_RowEditing" OnRowUpdating="UpdateCase_RowUpdating" OnRowCancelingEdit="updateCase_RowCancelingEdit"> <Columns> <asp:BoundField DataField="caseId" HeaderText="Saksnr" ItemStyle-Width="20" DataFormatString="Int" /> <asp:BoundField DataField="userName" HeaderText="Bruker" ItemStyle-Width="150" /> <asp:BoundField DataField="caseType" HeaderText="Type" ItemStyle-Width="150" /> <asp:BoundField DataField="caseRegby" HeaderText="Reg. av" ItemStyle-Width="60" /> <asp:BoundField DataField="caseTopic" HeaderText="Tittel" ItemStyle-Width="150" /> <asp:BoundField DataField="caseDesc" HeaderText="Beskrivelse" ItemStyle-Width="300" /> <asp:BoundField DataField="caseDtCreated" HeaderText="Opprettet dato" ItemStyle-Width="80" /> <asp:TemplateField HeaderText="Status" SortExpression="caseStatus" ItemStyle-Width="30"> <ItemTemplate> <%#IIf(Boolean.Parse(Eval("caseStatus").ToString()), "Lukket", "Åpen")%></ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Columns> </asp:GridView>

我试图使用的更新程序如下:

Protected Sub UpdateCase_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) sqlConnection.Open() sqlCommand = New SqlClient.SqlCommand("update TBL_CASE set TBL_CASE.caseType = @caseType, TBL_CASE.caseRegBy = @caseRegby, TBL_CASE.caseTopic = @caseTopic, TBL_CASE.caseDesc = @caseDesc where caseId = @caseID ") sqlCommand.Connection = sqlConnection Dim Id As Integer = caseList.Rows(e.RowIndex).Cells(0).Text Dim Type As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseType"), TextBox).Text Dim Regby As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseRegby"), TextBox).Text Dim Topic As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseTopic"), TextBox).Text Dim Desc As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseDesc"), TextBox).Text sqlCommand.Parameters.Add("@caseID", SqlDbType.Int).Value = Id sqlCommand.Parameters.Add("@caseType", SqlDbType.VarChar, 200).Value = Type sqlCommand.Parameters.Add("@caseRegby", SqlDbType.VarChar, 100).Value = Regby sqlCommand.Parameters.Add("@caseTopic", SqlDbType.VarChar, 300).Value = Topic sqlCommand.Parameters.Add("@caseDesc", SqlDbType.VarChar, 800).Value = Desc sqlCommand.ExecuteNonQuery() caseList.EditIndex = -1 sqlConnection.Close() BindData() End Sub

我不明白我写错了什么。 我看到如果我使用模板字段,我实际上已经能够更新数据库,但是在将数据显示到网格视图时会出现问题。

我的错误信息:

Object reference not set to an instance of an object.caseType Description: An unhandled exception occurred during the execution of the current web

请求。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Exception Details: System.Exception: Object reference not set to an instance of an object.caseType

来源错误:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

堆栈跟踪:

[Exception: Object reference not set to an instance of an object.caseType] Supportomatic_2000.Showcase.UpdateCase_RowUpdating(Object sender, GridViewUpdateEventArgs e) +1031 System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +139 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +808 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +855 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

到最后我看到,因为我正在使用标签:

<asp:BoundField DataField="caseType" HeaderText="Type" ItemStyle-Width="150" />

我不能使用fetch方法:

Dim Type As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseType"), TextBox).Text

但需要另辟蹊径,这是正确的吗? 如果是,那怎么样?

Im New to ASP.NET and VB:NET so im at a quit low Level now. Im trying to display a gridview using <asp:BoundField DataField..> or <asp:TemplateField..>. When using Boundfield, I am able to get the viewgrid to display what i want, but im not able to edit it. it tells me: "Input string was not in a correct format". When using Templates i have trouble even displaying the gridview.

If possible to use the gridview i get to work and be able to edit that one,, then I will stick to this format.

My DB looks like this:

[TBL_CASE]( [caseId] [int] IDENTITY(1,1) NOT NULL, [caseSub] [int] NULL, [caseUser] [int] NULL, [caseType] [varchar](200) NULL, [caseRegBy] [varchar](100) NULL, [caseTopic] [varchar](300) NULL, [caseDesc] [varchar](max) NULL, [caseDtCreated] [datetime] NULL, [caseStatus] [bit] NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My GridView like this, With a data source below to fetch the drop down that I use to sort out what is displayed:

<asp:GridView ID="caseList" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" OnRowEditing="editCase_RowEditing" OnRowUpdating="UpdateCase_RowUpdating" OnRowCancelingEdit="updateCase_RowCancelingEdit"> <Columns> <asp:BoundField DataField="caseId" HeaderText="Saksnr" ItemStyle-Width="20" DataFormatString="Int" /> <asp:BoundField DataField="userName" HeaderText="Bruker" ItemStyle-Width="150" /> <asp:BoundField DataField="caseType" HeaderText="Type" ItemStyle-Width="150" /> <asp:BoundField DataField="caseRegby" HeaderText="Reg. av" ItemStyle-Width="60" /> <asp:BoundField DataField="caseTopic" HeaderText="Tittel" ItemStyle-Width="150" /> <asp:BoundField DataField="caseDesc" HeaderText="Beskrivelse" ItemStyle-Width="300" /> <asp:BoundField DataField="caseDtCreated" HeaderText="Opprettet dato" ItemStyle-Width="80" /> <asp:TemplateField HeaderText="Status" SortExpression="caseStatus" ItemStyle-Width="30"> <ItemTemplate> <%#IIf(Boolean.Parse(Eval("caseStatus").ToString()), "Lukket", "Åpen")%></ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Columns> </asp:GridView>

the update procedure im trying to use is the following:

Protected Sub UpdateCase_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) sqlConnection.Open() sqlCommand = New SqlClient.SqlCommand("update TBL_CASE set TBL_CASE.caseType = @caseType, TBL_CASE.caseRegBy = @caseRegby, TBL_CASE.caseTopic = @caseTopic, TBL_CASE.caseDesc = @caseDesc where caseId = @caseID ") sqlCommand.Connection = sqlConnection Dim Id As Integer = caseList.Rows(e.RowIndex).Cells(0).Text Dim Type As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseType"), TextBox).Text Dim Regby As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseRegby"), TextBox).Text Dim Topic As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseTopic"), TextBox).Text Dim Desc As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseDesc"), TextBox).Text sqlCommand.Parameters.Add("@caseID", SqlDbType.Int).Value = Id sqlCommand.Parameters.Add("@caseType", SqlDbType.VarChar, 200).Value = Type sqlCommand.Parameters.Add("@caseRegby", SqlDbType.VarChar, 100).Value = Regby sqlCommand.Parameters.Add("@caseTopic", SqlDbType.VarChar, 300).Value = Topic sqlCommand.Parameters.Add("@caseDesc", SqlDbType.VarChar, 800).Value = Desc sqlCommand.ExecuteNonQuery() caseList.EditIndex = -1 sqlConnection.Close() BindData() End Sub

I don't understand what I have been written wrong. I see that if I use Template Fields, I have actually been able to update the DB, but then have a problem showing the SQL data into the grid view.

My error message:

Object reference not set to an instance of an object.caseType 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.Exception: Object reference not set to an instance of an object.caseType

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[Exception: Object reference not set to an instance of an object.caseType] Supportomatic_2000.Showcase.UpdateCase_RowUpdating(Object sender, GridViewUpdateEventArgs e) +1031 System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +139 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +808 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +855 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

To the end i see that since I am using the tag:

<asp:BoundField DataField="caseType" HeaderText="Type" ItemStyle-Width="150" />

I cannot use the fetch Method:

Dim Type As String = DirectCast(caseList.Rows(e.RowIndex).FindControl("caseType"), TextBox).Text

But needs to do it another way, is this correct? If yes, then how?

最满意答案

我发现了我的问题,看起来,当我更改了我想要从boundfield更新到模板字段的字段的标记时,它的工作正常,如下面的标记:

<asp:TemplateField HeaderText="Henv. gjelder"> <EditItemTemplate> <asp:TextBox ID="caseType" runat="server" Text='<%#Eval("caseType")%>'/> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblcaseType" runat="server" Text='<%#Eval("caseType")%>'/> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtftrcaseType" runat="server"/> <asp:RequiredFieldValidator ID="rfvcaseType" runat="server" ControlToValidate="txtftrcaseType" Text="*" ValidationGroup="validation"/> </FooterTemplate> </asp:Templatefield>

I found out of my problem, and it seems like, when i changed the tag for the Fields I wanted to update from boundfield to template Field, it was working OK like the tags below:

<asp:TemplateField HeaderText="Henv. gjelder"> <EditItemTemplate> <asp:TextBox ID="caseType" runat="server" Text='<%#Eval("caseType")%>'/> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblcaseType" runat="server" Text='<%#Eval("caseType")%>'/> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtftrcaseType" runat="server"/> <asp:RequiredFieldValidator ID="rfvcaseType" runat="server" ControlToValidate="txtftrcaseType" Text="*" ValidationGroup="validation"/> </FooterTemplate> </asp:Templatefield>

更多推荐

本文发布于:2023-08-06 04:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   数据   列表中   Updating   dropdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!