PDA

View Full Version : مشکل در بخش ویرایش "ضروری"



moonfa1392
پنج شنبه 11 تیر 1394, 09:36 صبح
سلام
من در بخش ویرایش مشکل دارم. من این کد رو نوشتم برای حذف و ویرایش سطر ها:


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteMenu")
{
int index = Convert.ToInt32(e.CommandArgument);
int MnuID = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text);
string sqltext = string.Format("delete from Tbl_Menu where Mnu_ID={0}", MnuID);
DAL run = new DAL();
run.ExecNonQuery(sqltext);
alert.InnerText = ("عملیات حذف با موفقیت انجام شد");
}
else if (e.CommandName == "EditMenu")
{
int index = Convert.ToInt32(e.CommandArgument);
int MnuID = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text);
DataTable dt = new DataTable();
string sqltext = string.Format("select * from tbl_menu where Mnu_ID={0}", MnuID);
DAL run = new DAL();
dt = run.ExecuteQuery(sqltext);
if (dt.Rows.Count > 0)
{
MenuTitle.Value = dt.Rows[0]["Mnu_Title"].ToString();
MenuLink.Value = dt.Rows[0]["Mnu_Link"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DAL run = new DAL();
string sqltext = string.Format("update tbl_Menu set Mnu_Title=N'{0}',Mnu_Link=N'{1}'",
MenuTitle.Value, MenuLink.Value);
run.ExecNonQuery(sqltext);
alert.InnerText = ("تغییرات با موفقیت ذخیره شد");
MenuTitle.Value = "";
MenuLink.Value = "";
}


این هم محتوای تابع DAL هست که صدا کردم:


public class DAL
{
public DAL()
{

}
public void ExecNonQuery(string sqltext)
{
SqlConnection k = new SqlConnection();
k.ConnectionString = "Data Source=HABIB-PC\\MOONFA;Initial Catalog=SimOfl5ne.7&@jfERRRFfvmls;Integrated Security=True";
k.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = k;
cmd.CommandText = sqltext;
cmd.ExecuteNonQuery();
k.Close();

}
public DataTable ExecuteQuery(string sqltext)
{
SqlConnection k = new SqlConnection();
k.ConnectionString = "Data Source=HABIB-PC\\MOONFA;Initial Catalog=SimOfl5ne.7&@jfERRRFfvmls;Integrated Security=True";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = k;
da.SelectCommand.CommandText = sqltext;
da.Fill(dt);
return dt;

}
}

مشکل اینجاست که وقتی یک سطر رو ویرایش می کنم پیغام میده که تغییرات با موفقیت ذخیره شد. اما تمام سطر ها رو ویرایش می کنه نه فقط سطر مورد نظر رو. لطفا بگین مشکل من از کجاست. این هم کد WebUserControl هست:


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListMenu.ascx.cs" Inherits="Admin_Controls_ListMenu" %>
<div class="MainIndex">
<div class="MITitle">
<h3> ویرایش منو</h3>
</div>
<div class="Alert">
<p id="alert" runat="server">لطفا عملیات مورد نظر را انتخاب کنید. مراقب باشید به صورت اشتباه یک سطر را حذف نکنید.</p>
</div>
<div class="Box">
<div class="BOX">
<asp:GridView CssClass="mGrid" ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" DataKeyNames="Mnu_ID">
<AlternatingRowStyle CssClass="mGrid1" BackColor="White" />
<Columns>
<asp:BoundField DataField="Mnu_ID" HeaderText="کد منو" InsertVisible="False" ReadOnly="True" SortExpression="Mnu_ID" />
<asp:BoundField DataField="Mnu_Title" HeaderText="عنوان منو" SortExpression="Mnu_Title" />
<asp:BoundField DataField="Mnu_Link" HeaderText="لینک منو" SortExpression="Mnu_Link" />
<asp:ButtonField CommandName="EditMenu" HeaderText="ویرایش" Text="ویرایش" />
<asp:ButtonField CommandName="DeleteMenu" HeaderText="حذف" Text="حذف" />
</Columns>
<EditRowStyle BackColor="#2461BF" CssClass="mGrid2" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SimOfl5ne.7&@jfERRRFfvmlsConnectionString %>" SelectCommand="SELECT [Mnu_Title], [Mnu_Link], [Mnu_ID] FROM [Tbl_Menu]"></asp:SqlDataSource>
<input type="text" id="MenuTitle" runat="server" class="MnuTitle" placeholder="نام منو" />
<input type="text" id="MenuLink" runat="server" class="MnuLink" placeholder="لینک منو (آدرس)" />
<asp:Button ID="Button1" CssClass="Button1" runat="server" Text="ویرایش منو" OnClick="Button1_Click" />
</div>
</div>
</div>