PDA

View Full Version : مشکل در update اطلاعات با formView.



hdv212
دوشنبه 15 مرداد 1386, 03:30 صبح
سلام و خسته نباشید
من توی صفحه ام، یه datagridView دارم و یه formView که با Select کردن هر رکورد از gridView، اطلاعات اون رکورد در formView ظاهر میشه تا کاربر در صورت تمایل، اون رو ویرایش کنه، حالا بعد از ویرایش، وقتی روی دکمه ی update در formView کلیک میکنه، پیغام خطایی میده مبنی بر اینکه دستور update معتبری لازم داره، دستور update رو با
یه sp ست کردم و پارامترهاشم تنظیم کردم و بهش اضافه کردم، ولی پیغام خطا میده که نمیتونم از sqlclient.sqlparameter استفاده کنم، شما چطوری از formView استفاده میکنید ؟

ClaimAlireza
دوشنبه 15 مرداد 1386, 08:27 صبح
میشه کدتون رو بزارین.....

hdv212
سه شنبه 16 مرداد 1386, 01:57 صبح
اینم کد :

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConnectionString"].ConnectionString);
SqlCommand cmd_update = new SqlCommand();
cmd_update.CommandType = CommandType.StoredProcedure;
cmd_update.Connection = con;
cmd_update.CommandText = "sp_UpdateNews";
SqlParameter p1 = new SqlParameter("@newsDate", this.FormView1.Row.Cells[3].Text);
SqlParameter p2 = new SqlParameter("@newsTitle", this.FormView1.Row.Cells[1].Text);
SqlParameter p3 = new SqlParameter("@newsBody", this.FormView1.Row.Cells[2].Text);
SqlParameter p4 = new SqlParameter("@newsResource", this.FormView1.Row.Cells[4].Text);
SqlParameter p5 = new SqlParameter("@newsID", this.FormView1.Row.Cells[0].Text);
cmd_update.Parameters.Add(p1);
cmd_update.Parameters.Add(p2);
cmd_update.Parameters.Add(p3);
cmd_update.Parameters.Add(p4);
cmd_update.Parameters.Add(p5);

try
{
con.Open();
cmd_update.ExecuteNonQuery();
Server.Transfer("admin.aspx");
this.lbl_Status.Text = "خبر جدید ثبت شد";
}
catch (StackOverflowException se)
{

}
finally
{
con.Close();
}

hdv212
سه شنبه 16 مرداد 1386, 14:14 عصر
کسی نبود ؟

ClaimAlireza
چهارشنبه 17 مرداد 1386, 15:04 عصر
این قسمت کدهای شما که به ظاهر مشکلی نداره.

ولی این کدها به درستی کار میکنن....



<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="gridviewformview.aspx.cs"Inherits="datalist_paging_gridviewformview" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="t_id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundFieldDataField="t_id"HeaderText="t_id"InsertVisible="False"ReadOnly="True"
SortExpression="t_id"/>
<asp:BoundFieldDataField="tname"HeaderText="tname"SortExpression="tname"/>
<asp:BoundFieldDataField="tfname"HeaderText="tfname"SortExpression="tfname"/>
<asp:CommandFieldShowSelectButton="True"/>
</Columns>
</asp:GridView>
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [tb_test]"></asp:SqlDataSource>
<asp:FormViewID="FormView1"runat="server"DataKeyNames="t_id"DataSourceID="SqlDataSource2">
<EditItemTemplate>
t_id:
<asp:LabelID="t_idLabel1"runat="server"Text='<%# Eval("t_id") %>'></asp:Label><br/>
tname:
<asp:TextBoxID="tnameTextBox"runat="server"Text='<%# Bind("tname") %>'>
</asp:TextBox><br/>
tfname:
<asp:TextBoxID="tfnameTextBox"runat="server"Text='<%# Bind("tfname") %>'>
</asp:TextBox><br/>
<asp:LinkButtonID="UpdateButton"runat="server"CausesValidation="True"CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButtonID="UpdateCancelButton"runat="server"CausesValidation="False"CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
tname:
<asp:TextBoxID="tnameTextBox"runat="server"Text='<%# Bind("tname") %>'>
</asp:TextBox><br/>
tfname:
<asp:TextBoxID="tfnameTextBox"runat="server"Text='<%# Bind("tfname") %>'>
</asp:TextBox><br/>
<asp:LinkButtonID="InsertButton"runat="server"CausesValidation="True"CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButtonID="InsertCancelButton"runat="server"CausesValidation="False"CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
t_id:
<asp:LabelID="t_idLabel"runat="server"Text='<%# Eval("t_id") %>'></asp:Label><br/>
tname:
<asp:LabelID="tnameLabel"runat="server"Text='<%# Bind("tname") %>'></asp:Label><br/>
tfname:
<asp:LabelID="tfnameLabel"runat="server"Text='<%# Bind("tfname") %>'></asp:Label><br/>
<asp:LinkButtonID="EditButton"runat="server"CausesValidation="False"CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<asp:LinkButtonID="DeleteButton"runat="server"CausesValidation="False"CommandName="Delete"
Text="Delete">
</asp:LinkButton>
<asp:LinkButtonID="NewButton"runat="server"CausesValidation="False"CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
DeleteCommand="DELETE FROM [tb_test] WHERE [t_id] = @t_id"InsertCommand="INSERT INTO [tb_test] ([tname], [tfname]) VALUES (@tname, @tfname)"
SelectCommand="SELECT * FROM [tb_test] WHERE ([t_id] = @t_id)"UpdateCommand="UPDATE [tb_test] SET [tname] = @tname, [tfname] = @tfname WHERE [t_id] = @t_id">
<DeleteParameters>
<asp:ParameterName="t_id"Type="Int64"/>
</DeleteParameters>
<UpdateParameters>
<asp:ParameterName="tname"Type="String"/>
<asp:ParameterName="tfname"Type="String"/>
<asp:ParameterName="t_id"Type="Int64"/>
</UpdateParameters>
<SelectParameters>
<asp:ControlParameterControlID="GridView1"Name="t_id"PropertyName="SelectedValue"
Type="Int64"/>
</SelectParameters>
<InsertParameters>
<asp:ParameterName="tname"Type="String"/>
<asp:ParameterName="tfname"Type="String"/>
</InsertParameters>
</asp:SqlDataSource>
&nbsp;
</div>
</form>
</body>
</html>

araz_pashazadeh
چهارشنبه 13 خرداد 1388, 22:20 عصر
دوست عزيز فكر مي كنم مشكل شما در قسمت SqlDataSource برنامه هستش شما در خاصيتهاي SqlDataSource در قسمت update query دستورات خودتو چك بكن