我已尝试过在线提供的所有帮助,但仍然无法使用我的代码解决此问题。我尝试了一切。请查看我的代码,找出问题所在。提前谢谢。
<asp:GridView ID="GridView1" runat="server" onrowcancelingedit="GridView1_RowCanceling" onrowediting="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowDeleted="GridView1_RowDeleted" OnRowUpdating="GridView1_RowUpdating" OnRowUpdated="GridView1_RowUpdated" width="680px" style="line-height: 180%;" DataKeyNames="NoteId" autogeneratecolumns="False"
emptydatatext="No notes." showfooter="True" DataSourceID="SqlDataSource1" AlternatingRowStyle-CssClass="alt" CssClass="mGrid" CellPadding="14" ForeColor="#333333" GridLines="None" >
<headerstyle backcolor="#5D7B9D" forecolor="MediumBlue"/>
<EditRowStyle BackColor="#999999" />
<footerstyle backcolor="#5D7B9D" forecolor="White" Font-Bold="True"/>
<AlternatingRowStyle CssClass="alt" BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:BoundField HeaderText="NoteId" DataField="NoteId" ReadOnly="true" visible="false" />
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this note?');" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Name" >
<EditItemTemplate>
<asp:label ID="linktextbox3" runat="server" Text='<%# Bind("Name")%>'></asp:label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="linklabel3" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="135px" HeaderText="Date" >
<EditItemTemplate>
<asp:label ID="TextBox3" runat="server" Text='<%# Bind("NoteDate", "{0:dd/MM/yyyy HH:mm:tt}")%>'></asp:label>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("NoteDate", "{0:dd/MM/yyyy h:mm:tt}")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="360px" HeaderText="Note" >
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" style="border:1px solid black;" TextMode="MultiLine" Width="350px" height="34px" Text='<%# Eval("Note")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Note")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AssignConnectionString %>" deletecommand="deleteNote" DeleteCommandType="StoredProcedure" UpdateCommand="updateNote" UpdateCommandType="StoredProcedure">
</asp:SqlDataSource>
代码背后的代码:
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim Note2 As TextBox = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox)
Dim conStr As String = ConfigurationManager.ConnectionStrings("assignConnectionString").ConnectionString
Dim cn As New SqlConnection(conStr)
cn.Open()
Dim updCmd As New SqlCommand("updateNote", cn)
updCmd.CommandType = CommandType.StoredProcedure
updCmd.Parameters.Add("@NoteId", SqlDbType.Int).Value = GridView1.Rows(e.RowIndex).Cells(0).Text
updCmd.Parameters.Add("@Note", SqlDbType.VarChar).Value = Note2.Text
SqlDataSource1.DataBind()
updCmd.Parameters.Clear()
bindGridView()
End Sub
存储过程:
ALTER PROCEDURE [dbo].[updateNote]
-- Add the parameters for the stored procedure here
@NoteId int,
@Note varchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Update Notes Set Note = @Note Where NoteId = @NoteId
END
答案 0 :(得分:1)
不确定,但在此行中尝试使用nvarchar而不是varchar
updCmd.Parameters.Add("@Note", SqlDbType.VarChar).Value = Note2.Text
它会喜欢
updCmd.Parameters.Add("@Note", SqlDbType.NVarChar).Value = Note2.Text
您编辑的代码背后:
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim Note2 As TextBox = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox)
Dim conStr As String = ConfigurationManager.ConnectionStrings("assignConnectionString").ConnectionString
Dim cn As New SqlConnection(conStr)
cn.Open()
Dim updCmd As New SqlCommand("updateNote", cn)
updCmd.CommandType = CommandType.StoredProcedure
updCmd.Parameters.Add("@NoteId", SqlDbType.Int).Value = GridView1.Rows(e.RowIndex).Cells(0).Text
updCmd.Parameters.Add("@Note", SqlDbType.VarChar).Value = Note2.Text
SqlDataSource1.DataBind()
updCmd.Parameters.Clear()
bindGridView()
End Sub
答案 1 :(得分:0)
检查下面代码中的数据类型:
updCmd.Parameters.Add("@Note", SqlDbType.VarChar).Value = Note2.Text
检查StoredProcedure中的数据类型
@Note nvarchar(50)
在两端使用nvarchar
答案 2 :(得分:0)
我通过将gridview中的所有字段更改为“Eval”而不是“Bind”解决了这个问题,除了我想要更新的字段。这与其他解决方案完全相反,他们说除了你想要使用的字段之外,他们都会将所有内容更改为Bind。我的网格视图是我如何做到的:
<asp:GridView ID="GridView1" runat="server" onrowcancelingedit="GridView1_RowCanceling" onrowediting="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowDeleted="GridView1_RowDeleted" OnRowUpdating="GridView1_RowUpdating" OnRowUpdated="GridView1_RowUpdated" width="680px" style="line-height: 180%;" autogeneratecolumns="False"
DataKeyNames="NoteId" emptydatatext="No notes." showfooter="True" DataSourceID="SqlDataSource1" AlternatingRowStyle-CssClass="alt" CssClass="mGrid" CellPadding="14" ForeColor="#333333" GridLines="None" >
<headerstyle backcolor="#5D7B9D" forecolor="MediumBlue"/>
<EditRowStyle BackColor="#999999" />
<footerstyle backcolor="#5D7B9D" forecolor="White" Font-Bold="True"/>
<AlternatingRowStyle CssClass="alt" BackColor="White" ForeColor="#284775"></AlternatingRowStyle>
<Columns>
<asp:BoundField HeaderText="NoteId" DataField="NoteId" ReadOnly="true" visible="False" />
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this note?');" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="100px" HeaderText="Name" >
<EditItemTemplate>
<asp:label ID="linktextbox3" runat="server" Text='<%# Eval("Name")%>'></asp:label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="linklabel3" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="135px" HeaderText="Date" >
<EditItemTemplate>
<asp:label ID="TextBox3" runat="server" Text='<%# Eval("NoteDate", "{0:dd/MM/yyyy HH:mm:tt}")%>'></asp:label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("NoteDate", "{0:dd/MM/yyyy h:mm:tt}")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="360px" HeaderText="Note" >
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" style="border:1px solid black;" TextMode="MultiLine" Width="350px" height="34px" Text='<%# Bind("Note")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Note")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
另外,我必须为sqldatasource添加一个更新参数,如下所示,仅用于我正在更新的文本框:
<asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AssignConnectionString %>" deletecommand="deleteNote" DeleteCommandType="StoredProcedure" UpdateCommand="updateNote" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Note" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>