在gridview中更新数据
我想获得带有image的编辑数据。我有像db,image,name,price,detail这样的数据库字段(pid,pimg,pname,price,pdesc)。我有gridview的编辑链接,如果按下它,我得到一个包含fileupload的面板,在特定字段中修改的文本框控件。我可以通过以下代码在所有字段中进行更改但是如果我按下该编辑链接并且如果我不想更改任何数据然后按下按钮(编辑按钮我必须得到更改)m得到错误。 Plz帮我这个.... !!这里是我的.cs文件
I wanna get edited data with image.I have db fields like id,image,name,price,detail i.e.(pid,pimg,pname,price,pdesc).I have edit link with gridview and if I press it,am getting one panel which contains the fileupload,textboxes controls for modification in particular field. I can do the changes in all the fields by this following code but if i press that edit link and if I don''t want to change any of the data then by pressing Button (Edit Button i have to get change) m getting error. Plz help me with this....!!here is my .cs file
using System;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ed")
{
int pid = Convert.ToInt32(e.CommandArgument);
SqlConnection con;
SqlCommand com;
SqlDataReader red;
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
com = con.CreateCommand();
com.CommandText = "select * from table_name where pid=@pid";
com.Parameters.AddWithValue("@pid",pid);
con.Open();
red = com.ExecuteReader();
red.Read();
// FileUpload1.FileName = red["pimg"].ToString();
TextBox6.Text = red["pimg"].ToString();
TextBox3.Text = red["pname"].ToString();
TextBox4.Text = red["price"].ToString();
TextBox5.Text = red["pdesc"].ToString();
red.Close();
con.Close();
Label1.Text = pid.ToString();
Panel1.Visible = true;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int pid = int.Parse(Label1.Text);
SqlConnection con;
SqlCommand com;
int r = 0;
string path = Server.MapPath("~/Products");
FileUpload1.SaveAs(path + "/" + FileUpload1.FileName);
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
com = con.CreateCommand();
if (FileUpload1.FileName != null)
{
com.CommandText = "update table_name set pimg=@pimg,pname=@pname,price=@price,pdesc=@pdesc where pid=@pid";
string nm = "./Products/" + FileUpload1.FileName;
com.Parameters.AddWithValue("@pimg", nm);
com.Parameters.AddWithValue("@pid", pid);
//com.Parameters.AddWithValue("@pimg",TextBox6.Text.ToString());
com.Parameters.AddWithValue("@pname", TextBox3.Text.ToString());
com.Parameters.AddWithValue("@price", TextBox4.Text.ToString());
com.Parameters.AddWithValue("@pdesc", TextBox5.Text.ToString());
// com.Parameters.AddWithValue("@pimg", FileUpload1.FileName.ToString());
}
else
{
com.CommandText = "update table_name set pname=@pname,price=@price,pdesc=@pdesc where pid=@pid";
com.Parameters.AddWithValue("@pid",pid);
com.Parameters.AddWithValue("@pname",TextBox3.Text.ToString());
com.Parameters.AddWithValue("@price",TextBox4.Text.ToString());
com.Parameters.AddWithValue("@pdesc",TextBox5.Text.ToString());
}
con.Open();
r = com.ExecuteNonQuery();
con.Close();
Panel1.Visible = false;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
}
}
here is my default.aspx file....
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="image" SortExpression="pid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("pid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="50px"
ImageUrl='<%# Eval("pimg") %>' Width="50px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="pname" HeaderText="pname" SortExpression="pname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="pdesc" HeaderText="pdesc" SortExpression="pdesc" />
<asp:TemplateField HeaderText="edit" SortExpression="pid">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("pid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("pid") %>' CommandName="ed">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [table_name]"></asp:SqlDataSource>
<br />
<asp:Panel ID="Panel1" runat="server">
<table style="width:100%;">
<tr>
<td class="style1">
Prpduct Image</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
Product Name</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style1">
Product Price</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
Product Desc</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Edit" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Cancel" />
</td>
</tr>
</table>
</asp:Panel>
ConnectionStrings:ConnectionString%>
SelectCommand = SELECT * FROM [table_name] > < / asp:SqlDataSource >
< br />
< asp:Panel ID = Panel1 runat = server >
< table style = width:100%; >
< tr>
< td class = style1 >
Prpduct图片< / td >
< td>
< asp:FileUpload ID = FileUpload1 runat = server />
< asp:TextBox ID = TextBox6 runat = server > < / asp:TextBox >
< / td >
< / tr >
< ; TR>
< td class = style1 >
产品名称< / td >
< td>
< asp:TextBox ID = TextBox3 runat = server > < / asp:TextBox >
< asp:Label ID = Label1 runat = server Text = Label > < / asp:Label >
< / td >
< / tr >
< tr>
< td class = style1 >
产品价格< / td >
< td>
< asp:TextBox ID = TextBox4 runat = server > < / asp:TextBox >
< / td >
< / tr >
< ; TR>
< td class = style1 >
产品描述< / td >
< td>
< asp:TextBox ID = TextBox5 runat = server > < / asp:TextBox >
< / td >
< / tr >
< ; TR>
< td class = style1 >
< asp:按钮ID = Button1 runat = server onclick = Button1_Click Text = 编辑 />
< asp:按钮ID = Button2 runat = server onclick = Button2_Click Text = 取消 />
< / td >
< / tr >
< / table >
< / asp:Panel >
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [table_name]"></asp:SqlDataSource> <br /> <asp:Panel ID="Panel1" runat="server"> <table style="width:100%;"> <tr> <td class="style1"> Prpduct Image</td> <td> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style1"> Product Name</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td class="style1"> Product Price</td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style1"> Product Desc</td> <td> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style1"> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Edit" /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Cancel" /> </td> </tr> </table> </asp:Panel>