با سلام
من سایتی درمورد رستوران با زیان asp.net دارم مینویسم و تازه کار هم هستم.
میخام در قسمت منوی غذاها وقتی روی دکمه ی افزودن کلیک کرد(منوی غذا ها را با اسفاده گرید ویو نمایش دادم)،ان سطر از گرید ویو را داخل جدولی در بانک اطلاعاتیم به همراه نام کاربری کاربر ذخیره کند.
این کار به چه شکل است؟ممنون میشم کدش را به زبان C# در اختیارم بگذارید.
لطفا عنوان مناسبی برای تاپیک هاتون قرار دهید و متاسفانه از دفعه بعد حذف خواهند شد .
شما میتونید در رویداد RowCommand دکمه زده شده را کنترل کنید و کدهای ذخیره در بانک را بنویسید
برای دکمه در گرید یک CommandArgument بزارید که کد غذا هست و موقع ذخیره بدست بیارید و ذخیره کنید
با سلام،ممنون از پاسختون.ولی من چیزی متوجه نشدم.همانطور که عرض کردم میخام وقتی روی دکمه ی پلاس همانطور که تو عکس میبینید اطلاعات ان سطر از گرید ویو به همراه ایدی کاربر داخل جدول ذخیره کنم
<asp:TemplateField> <ItemTemplate> <asp:Button ID="AddButton" runat="server" CommandName="AddToCart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" /> </ItemTemplate> </asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index]; // Add code here to add the item to the shopping cart. } }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "AddToCart") { string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["webdatabaseconnection"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index]; // Add code here to add the item to the shopping cart. string query = "insert into tbl_News(Title,Abstrac,Contents,Pic) values(@Title,@Abstrac,@Contents,@Pic)"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@Title", row); cmd.Parameters.AddWithValue("@Abstrac", row); cmd.Parameters.AddWithValue("@Contents", row); cmd.Parameters.AddWithValue("@Pic", row); con.Open(); if (cmd.ExecuteNonQuery() > 0) { Label5.Text = "succsssfully..."; } con.Close(); } }
با سلام وقتتون بخیر،ممنون که پاسخ میدید و همچنین عذر خواهی میکنم بابت اینکه انقدر سوال میپرسم.
ببینید طبق عرایضی که فرمودید اینطور کد نوشتم و نمیدونم به جای دو تا کامنت اولیتون چی باید بنویسم.
و اصلا اینطور نحوه ی insert کردن درست هست؟
بله درسته
شماره خط را بنویسید
منظورتون از کامنت کدام خطهاست ؟
منظورم از کامت خط شماره ی (6و7)و(10و11) هست
چون الان با این قطعه کدی که نوشتم اصلا اطلاعاتی درج نمیکنه
چیزی نباید بنویسید
اونها توضیح هستند
اخه هیچ خروجی ندارم.(error هم ندارم)فکر کردم خط کد اضافه تری هست که باید بنویسم.
با سلام.ممنون بابت راهنمای هاتون.
الان تنها مشکلی که دارم اینه که اطلاعات خالی در جدول وارد میکند.یعنی با کلیک کردن روی دکمه در جدول مورد نظرم سطری خالی ایجاد میکند.
اینم کدهاش:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="NewsID" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" > <Columns> <asp:TemplateField> <ItemTemplate> <table class="auto-style1"> <tr> <td><asp:Button runat="server" ID="btnPloss" Text="ploss" CommandName="AddToCart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/></td> <td> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>'></asp:Label> </td> </tr> <tr> <td> </td> <td> <asp:Label ID="Label2" runat="server" Text='<%# Eval("Abstrac") %>'></asp:Label> </td> </tr> <tr> <td> </td> <td> <asp:Label ID="Label3" runat="server" Text='<%# Eval("Contents") %>'></asp:Label> </td> </tr> <tr> <td> </td> <td> <asp:Label ID="Label4" runat="server" Text='<%# Eval("Pic") %>'></asp:Label> </td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index]; // Add code here to add the item to the shopping cart. string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["webdatabaseconnection"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); string query = "insert into tbl_News(Title,Abstrac,Contents,Pic) values(@Title,@Abstrac,@Contents,@Pic)"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@Title", row.Cells[0].Text); cmd.Parameters.AddWithValue("@Abstrac", row.Cells[0].Text); cmd.Parameters.AddWithValue("@Contents", row.Cells[0].Text); cmd.Parameters.AddWithValue("@Pic", row.Cells[0].Text); con.Open(); if (cmd.ExecuteNonQuery() > 0) { Label5.Text = "succsssfully..."; } con.Close(); } }
با سپاس فراوان
cmd.Parameters.AddWithValue("@Title", row.Cells[0].Text); cmd.Parameters.AddWithValue("@Abstrac", row.Cells[1].Text); cmd.Parameters.AddWithValue("@Contents", row.Cells[2].Text); cmd.Parameters.AddWithValue("@Pic", row.Cells[3].Text);
با سلام
چنین کدی هم نوشتم ولی با این خطا مواجه میشم:
Specified argument was out of the range of valid values. Parameter name: index
به تعداد ستونها گیر میده
ایندکس ها رو بر اساس ستونها تنظیم کنید
عذر میخام باید چطوری بر اساس ستون تنظیم کنم؟
با تشکر فراوان از پاسخ گویی سریعتان
تعداد ستونهای گرید را بشمارید و در Cell قرار دهید
فقط دقت کنید شماره ها 0 شروع میشه
مثلا ستون اول Cell صفرم است
بازم همون خطا را میده
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)