从代码后面更新gridview?

时间:2016-02-29 16:32:37

标签: c# asp.net

我正在尝试使用c#代码更新gridview。我通常使用SqlDataSource,但我需要捕获当前用户进行更改,但这似乎不起作用。所以我想我会在后面的代码中尝试一下。

以下是gridview:

<asp:GridView
ID="gv_rvw_sub"
runat="server"
AllowPaging="True"
AllowSorting="True"
DataSourceID="sdc_gv_rvw_sub"
CellPadding="4"
AutoGenerateColumns="False"
EmptyDataText="There are no records to display."
OnSelectedIndexChanged="gv_rvw_sub_SelectedIndexChanged"
OnRowUpdating="gv_rvw_sub_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True"></asp:CommandField>
<asp:BoundField DataField="doc_rvw_sub_id_pk" HeaderText="Submission" ReadOnly="True" InsertVisible="False" SortExpression="doc_rvw_sub_id_pk"></asp:BoundField>
<asp:BoundField DataField="doc_rvw_sub_by" HeaderText="Submitted By" SortExpression="doc_rvw_sub_by" ReadOnly="true"></asp:BoundField>
<asp:BoundField DataField="doc_rvw_sub_recip_list" HeaderText="Recipient" SortExpression="doc_rvw_sub_recip_list" ReadOnly="true" />
<asp:BoundField DataField="doc_rvw_sub_dt" HeaderText="Date Submitted" SortExpression="doc_rvw_sub_dt" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true"></asp:BoundField>
<asp:BoundField DataField="doc_rvw_sub_due_dt" HeaderText="Due Date" SortExpression="doc_rvw_sub_due_dt" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true" />
<asp:TemplateField AccessibleHeaderText="Review Status" HeaderText="Review Status" SortExpression="doc_rvw_sub_status_list">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="True"
DataSourceID="sdc_doc_rvw_sub_status_list"
DataTextField="rvw_status"
DataValueField="rvw_status_id_pk"
AppendDataBoundItems="true"
SelectedValue='<%# Bind("doc_rvw_sub_status_list") %>'>
<asp:ListItem Value="">Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("rvw_status") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Status Set By" SortExpression="doc_rvw_sub_status_set_by">
<EditItemTemplate>
<%--<asp:Label runat="server" Text='<%# Eval("doc_rvw_sub_by") %>' ID="Label1"></asp:Label>--%>
<asp:TextBox ID="status_set_by_txt" runat="server" Text='<%# Eval("doc_rvw_sub_status_set_by") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("doc_rvw_sub_status_set_by") %>' ID="Label2"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="doc_rvw_sub_status_dt_set" HeaderText="Status Date" SortExpression="doc_rvw_sub_status_dt_set" DataFormatString="{0:MM/dd/yyyy}" ReadOnly="true" />
<asp:BoundField Visible="false" DataField="doc_rvw_id_fk" HeaderText="Document ID" SortExpression="doc_rvw_id_fk" ReadOnly="true"></asp:BoundField>
</Columns>
</asp:GridView>

这是SqlDataSource:

<asp:SqlDataSource
ID="sdc_gv_rvw_sub"
runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:idrfConnectionString %>"
InsertCommand="INSERT INTO [tbl_doc_rvw_sub] ([doc_rvw_sub_by], [doc_rvw_sub_recip_list], [doc_rvw_sub_dt], [doc_rvw_sub_status_list], [doc_rvw_id_fk]) VALUES (@doc_rvw_sub_by, @doc_rvw_sub_recip_list, @doc_rvw_sub_dt, @doc_rvw_sub_status_list, @doc_rvw_id_fk)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="
SELECT [doc_rvw_sub_id_pk]
, [doc_rvw_sub_by]
, [doc_rvw_sub_recip_list]
, [doc_rvw_sub_dt]
, [doc_rvw_sub_status_list]
, tsl.rvw_status
, [doc_rvw_id_fk] 
, [doc_rvw_sub_due_dt]
, [doc_rvw_sub_status_set_by]
, [doc_rvw_sub_status_dt_set]
FROM [tbl_doc_rvw_sub] 
LEFT JOIN tbl_status_list tsl 
ON tbl_doc_rvw_sub.doc_rvw_sub_status_list=tsl.rvw_status_id_pk 
WHERE ([doc_rvw_id_fk] = @doc_rvw_id_fk)">
<SelectParameters>
<asp:ControlParameter ControlID="gv_doc_rvw" Name="doc_rvw_id_fk" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

以下是代码:

protected void gv_rvw_sub_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection idrf_cnxn = new SqlConnection("Data Source=WDBSVCPRD01\\SVCDB;Initial Catalog=idrf;Integrated Security=True");
{
SqlCommand updt_sub_rvw_cmd = new SqlCommand("UPDATE tbl_doc_rvw_sub(doc_rvw_sub_status_list, doc_rvw_sub_status_set_by, doc_rvw_sub_status_dt_set) WHERE (doc_rvw_sub_status_list=@doc_rvw_sub_status_list, doc_rvw_sub_status_set_by=@doc_rvw_sub_status_set_by, doc_rvw_sub_status_dt_set=@doc_rvw_sub_status_dt_set)", idrf_cnxn);
updt_sub_rvw_cmd.Parameters.AddWithValue("@doc_rvw_sub_status_list", gv_rvw_sub.FindControl("DropDownList1"));
updt_sub_rvw_cmd.Parameters.AddWithValue("@doc_rvw_sub_status_set_by", HttpContext.Current.User.Identity.Name);
updt_sub_rvw_cmd.Parameters.AddWithValue("@doc_rvw_sub_status_dt_set", DateTime.Now.ToString());

idrf_cnxn.Open();
updt_sub_rvw_cmd.ExecuteNonQuery();
idrf_cnxn.Close();

if (IsPostBack)
{
gv_doc_rvw.DataBind();
}
}
}

问题是我一直收到以下错误 - “'''''

附近的语法不正确

我哪里出错了?有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

SQL中的更新语句语法是

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

我认为你的更新声明应该是

UPDATE tbl_doc_rvw_sub SET doc_rvw_sub_status_list=@doc_rvw_sub_status_list, doc_rvw_sub_status_set_by=@doc_rvw_sub_status_set_by, doc_rvw_sub_status_dt_set=@doc_rvw_sub_status_dt_set WHERE doc_rvw_sub_status_list=@doc_rvw_sub_status_list, doc_rvw_sub_status_set_by=@doc_rvw_sub_status_set_by, doc_rvw_sub_status_dt_set=@doc_rvw_sub_status_dt_set