PDA

View Full Version : ایجاد دکمه edit,cancel, update



samira83
چهارشنبه 17 مرداد 1386, 08:45 صبح
در مد update, به منظور ایجاد دکمه updateاین errorرا میبینمو برای رفع آن چکار باید بکنم؟چطور باید از controls استفاده کنموتا متن dbgridبه حالت ویرایش در آید؟

Specified cast is not valid.
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.InvalidCastException: Specified cast is not valid.
Source Error:
Line 587: updCommand.Connection=sqlConnection1;
Line 588: updCommand.CommandText="update doc_time set ";
Line 589: updCommand.CommandText+=" am1="+ Convert.ToInt32((TextBox)e.Item.Cells[6].Controls[0]);
Line 590: updCommand.CommandText+=",am2="+Convert.ToInt32((TextBox)e.Item.Cells[7].Controls[0]);

maxpayn2
چهارشنبه 17 مرداد 1386, 10:01 صبح
منظورت رو درست نفهمیدم ولی من یک گرید کامل نوشتم که برات میذارم ، امیدوارم به دردت بخوره :


<asp:GridViewID="GridView1"runat="server"DataKeyNames="cart_id"DataSourceID="ObjectDataSource1"
AllowPaging="True"AllowSorting="True"AutoGenerateColumns="False"
OnRowUpdated="GridView1_RowUpdated"OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowDeleting="GridView1_RowDeleting"OnRowDeleted="GridView1_RowDeleted"
OnRowCommand="GridView1_RowCommand"OnDataBound="GridView1_DataBound">


<Columns>



<asp:TemplateFieldHeaderText="product code">
<EditItemTemplate>
<asp:LabelID="lblUpdate"runat="server"Text='<%#Bind("product_id")%>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("product_id")%>


</ItemTemplate>



<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="ëںê ꥭیé">
<ItemTemplate>
<%#Eval("product_name")%>
</ItemTemplate>



<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="price">
<ItemTemplate>
<%#Eval("unit_price")%>
</ItemTemplate>



<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="weight">
<ItemTemplate>
<%#Eval("unit_weight")%>
</ItemTemplate>



<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
<asp:TemplateFieldHeaderText="qty">
<EditItemTemplate>
<asp:TextBoxID="txtUpdate"runat="server"Text='<%#Bind("qty")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("qty")%>
</ItemTemplate>



<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
<asp:CommandFieldShowEditButton="True"ButtonType="Button"UpdateText="submit"CancelText="cancel"
EditText="update"HeaderText="update">
<FooterStyleHorizontalAlign="Center"Font-Names="Tahoma"/>
<HeaderStyleHorizontalAlign="Center"Font-Names="Tahoma"/>
<ItemStyleHorizontalAlign="Center"Font-Names="Tahoma"/>
</asp:CommandField>
<asp:TemplateFieldHeaderText="delete">


<ItemTemplate>
<asp:ButtonID="btndelete"runat="server"Text="delete"CommandArgument='<%#Eval("product_id")%>'CausesValidation="false"OnClick="btndelete_Click"/>
</ItemTemplate>
<ItemStyleFont-Names="Tahoma"/>
<HeaderStyleFont-Names="Tahoma"/>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table>
<tr>
<thstyle="font-family:Tahoma">code</th>
<thstyle="font-family:Tahoma">name</th>
<thstyle="font-family:Tahoma">price</th>
<thstyle="font-family:Tahoma">weight</th>
<thstyle="font-family:Tahoma">qty</th>





</tr>

<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>


</EmptyDataTemplate>
</asp:GridView>

samira83
چهارشنبه 17 مرداد 1386, 10:32 صبح
ممنون از پاسختون. من یک دکمه edit در هر سطر ِDataGrid گذاشتم که بعد از زدن دکمه سطر مربوطه به حالت ویرایش در می آید. حال برای ثبت تغیرات در db,باید از روی سطر خوانده شود و در db ذخیره شود.ولی نمی توان از خاصیت text استفاده کرد.باید ازاستفاده کردواما errorمیدهد
من با asp وc# میکنم.htmlبلد نیستم.
[ TextBox)e.Item.Cells[6].Controls[0

maxpayn2
چهارشنبه 17 مرداد 1386, 12:02 عصر
اگه کد رو کامل مطالعه کرده باشی میبینی من هم همین کار رو کردم و چون فقط میخوام اجازه بدم که تعداد محصول عوض بشه در ستون مربوط به تعداد (qty) این کد رو اضافه کردم :









<EditItemTemplate>


<asp:TextBoxID="txtUpdate"runat="server"Text='<%#Bind("qty")%>'></asp:TextBox>
</EditItemTemplate>















حالا کافیه در sqldatasource که برای گرید انتخاب کردی updatecommand رو ست کنی و براش متغیر تعریف کنی ، به این صورت :




<asp:SqlDataSourceID="sqldatasource1"runat="server"ConnectionString="<%$ ConnectionStrings:alvercoConnectionString %>"
UpdateCommand="UPDATE dbo.shoppingcart SET qty = @qty WHERE (cart_id = @cart_id) AND (product_id = @product_id)">
<UpdateParameters>
<asp:ParameterName="qty"Type="Int32"/>
<asp:ParameterName="cart_id"Type="Int32"/>
<asp:ParameterName="product_id"Type="Int32"/>
</UpdateParameters>
</asp:SqlDataSource>

majid.nozad
پنج شنبه 25 مرداد 1386, 21:31 عصر
البته من دقیق نفهمیدم مشکل کجاست ولی معمولا خطایcast زمانی ظاهر میشه که نوع فیلد داخل دیتابیس با نوعی که شما وارد می کنید یکی نیست و یا null داره ذخیره میشه