rational unified process
پنج شنبه 08 اسفند 1392, 03:26 صبح
سلام
خیلی ساده بگم.من یه جدول tbl_temp دارم که دو تا رکورد درج شده توش.یک صفحه ساده aspx دارم که یه gridview داره که به اون جدول وصله و دو تا template field برای کامنت و تعداد هر محصول به گرید ویو اضافه کرده ام.
مشخصات جدول
http://upload7.ir/viewer.php?file=49075714170016102264.jpg
صفحه aspx :
<%@ Page Language="C# " AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="ProductionID" HeaderText="ProductionID"
SortExpression="ProductionID" />
<asp:BoundField DataField="ProductionCategoryID"
HeaderText="ProductionCategoryID" SortExpression="ProductionCategoryID" />
<asp:BoundField DataField="ProductionName" HeaderText="ProductionName"
SortExpression="ProductionName" />
<asp:BoundField DataField="Cast" HeaderText="Cast" SortExpression="Cast" />
<asp:BoundField DataField="IP" HeaderText="IP" SortExpression="IP" />
<asp:TemplateField HeaderText="Comments" SortExpression="Comments">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Comments") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="com" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Numbers" SortExpression="Numbers">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Numbers") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="num" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
DeleteCommand="DELETE FROM [tbl_Temp] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [tbl_Temp] ([ProductionID], [ProductionCategoryID], [ProductionName], [Cast], [IP], [Comments], [Numbers]) VALUES (@ProductionID, @ProductionCategoryID, @ProductionName, @Cast, @IP, @Comments, @Numbers)"
SelectCommand="SELECT * FROM [tbl_Temp]"
UpdateCommand="UPDATE [tbl_Temp] SET [ProductionID] = @ProductionID, [ProductionCategoryID] = @ProductionCategoryID, [ProductionName] = @ProductionName, [Cast] = @Cast, [IP] = @IP, [Comments] = @Comments, [Numbers] = @Numbers WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductionID" Type="Int32" />
<asp:Parameter Name="ProductionCategoryID" Type="Int32" />
<asp:Parameter Name="ProductionName" Type="String" />
<asp:Parameter Name="Cast" Type="Int32" />
<asp:Parameter Name="IP" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="Numbers" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductionID" Type="Int32" />
<asp:Parameter Name="ProductionCategoryID" Type="Int32" />
<asp:Parameter Name="ProductionName" Type="String" />
<asp:Parameter Name="Cast" Type="Int32" />
<asp:Parameter Name="IP" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="Numbers" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
صفحه کد بیهایند:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["cn"].ConnectionString);
string query = "update tbl_temp set comments=@comments,numbers=@numbers where productionID=@id";
SqlCommand cm = new SqlCommand(query, cn);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox txtHowMany = (TextBox)GridView1.Rows[i].FindControl("num");
TextBox tbx = (TextBox)GridView1.Rows[i].FindControl("com");
cm.Parameters.AddWithValue("comments", tbx.Text);
cm.Parameters.AddWithValue("numbers", txtHowMany.Text);
cm.Parameters.AddWithValue("id", GridView1.Rows[i].Cells[2].Text);
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
}
}
}
برنامه رو اجرا میکنم و این عکسشه:
http://upload7.ir/imgs/2014-02/64600766112290866512.jpg
بعد تکست باکس های مربوط به کامنت و تعداد را وارد میکنم و دکمه را میزنم تا رویداد اون مبنی بر اپدیت اجرا شه . ولی نمیشه و این خطا میاد :
http://upload7.ir/imgs/2014-02/72076088468575978349.jpg
اما نکته ای که هست اینه که اگه یک رکورد داشته باشم براحتی با زدن دکمه ، اپدیت میشه.
خیلی ساده بگم.من یه جدول tbl_temp دارم که دو تا رکورد درج شده توش.یک صفحه ساده aspx دارم که یه gridview داره که به اون جدول وصله و دو تا template field برای کامنت و تعداد هر محصول به گرید ویو اضافه کرده ام.
مشخصات جدول
http://upload7.ir/viewer.php?file=49075714170016102264.jpg
صفحه aspx :
<%@ Page Language="C# " AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="ProductionID" HeaderText="ProductionID"
SortExpression="ProductionID" />
<asp:BoundField DataField="ProductionCategoryID"
HeaderText="ProductionCategoryID" SortExpression="ProductionCategoryID" />
<asp:BoundField DataField="ProductionName" HeaderText="ProductionName"
SortExpression="ProductionName" />
<asp:BoundField DataField="Cast" HeaderText="Cast" SortExpression="Cast" />
<asp:BoundField DataField="IP" HeaderText="IP" SortExpression="IP" />
<asp:TemplateField HeaderText="Comments" SortExpression="Comments">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Comments") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="com" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Numbers" SortExpression="Numbers">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Numbers") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="num" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
DeleteCommand="DELETE FROM [tbl_Temp] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [tbl_Temp] ([ProductionID], [ProductionCategoryID], [ProductionName], [Cast], [IP], [Comments], [Numbers]) VALUES (@ProductionID, @ProductionCategoryID, @ProductionName, @Cast, @IP, @Comments, @Numbers)"
SelectCommand="SELECT * FROM [tbl_Temp]"
UpdateCommand="UPDATE [tbl_Temp] SET [ProductionID] = @ProductionID, [ProductionCategoryID] = @ProductionCategoryID, [ProductionName] = @ProductionName, [Cast] = @Cast, [IP] = @IP, [Comments] = @Comments, [Numbers] = @Numbers WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductionID" Type="Int32" />
<asp:Parameter Name="ProductionCategoryID" Type="Int32" />
<asp:Parameter Name="ProductionName" Type="String" />
<asp:Parameter Name="Cast" Type="Int32" />
<asp:Parameter Name="IP" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="Numbers" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductionID" Type="Int32" />
<asp:Parameter Name="ProductionCategoryID" Type="Int32" />
<asp:Parameter Name="ProductionName" Type="String" />
<asp:Parameter Name="Cast" Type="Int32" />
<asp:Parameter Name="IP" Type="String" />
<asp:Parameter Name="Comments" Type="String" />
<asp:Parameter Name="Numbers" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
صفحه کد بیهایند:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["cn"].ConnectionString);
string query = "update tbl_temp set comments=@comments,numbers=@numbers where productionID=@id";
SqlCommand cm = new SqlCommand(query, cn);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox txtHowMany = (TextBox)GridView1.Rows[i].FindControl("num");
TextBox tbx = (TextBox)GridView1.Rows[i].FindControl("com");
cm.Parameters.AddWithValue("comments", tbx.Text);
cm.Parameters.AddWithValue("numbers", txtHowMany.Text);
cm.Parameters.AddWithValue("id", GridView1.Rows[i].Cells[2].Text);
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
}
}
}
برنامه رو اجرا میکنم و این عکسشه:
http://upload7.ir/imgs/2014-02/64600766112290866512.jpg
بعد تکست باکس های مربوط به کامنت و تعداد را وارد میکنم و دکمه را میزنم تا رویداد اون مبنی بر اپدیت اجرا شه . ولی نمیشه و این خطا میاد :
http://upload7.ir/imgs/2014-02/72076088468575978349.jpg
اما نکته ای که هست اینه که اگه یک رکورد داشته باشم براحتی با زدن دکمه ، اپدیت میشه.