This page is not pretty. I'm working on ways to get "photos" into an image column so I'm just hacking around with this page. This page has two buttons. One button Adds a new person and their photo, the other button will modify a person who already exists and replace their existing photo. But, what I can NOT figure out is that after I click the button to add a person and then refresh the page to see the results in the grid, the page does NOT refresh the grid, but it DOES add a second record. And every time i refresh the page it will add another record. Can anybody tell me what I'm doing wrong so the page refresh "re-executes" the on_click event code without anybody clicking the button? I would like for the page to re-load the grid with the updated data every time the button is pressed and clear everything out so duplicate data is not added. But I can NOT figure out what is going on here.
<code>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.IO "%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<script runat="server">
String GetUrl(Object name)
{
return "getphoto.aspx?name=" + name.ToString();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Stream imagestream;
int len = FileUpload1.PostedFile.ContentLength; // get length of the file
imagestream = FileUpload1.PostedFile.InputStream; // get stream for the image
Byte [] imagecontent = new Byte[len]; // create an array of bytes to hold image data
imagestream.Read(imagecontent, 0, len); // read image into array
SqlConnection con = new SqlConnection("Data Source=dev1\\sqlexpress;Initial Catalog=web;uid=write_images;password=write_images");
con.Open();
SqlCommand cmd = new SqlCommand("update persons set photo =(@photo) where name=@name",con);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = TextBox1.Text;
cmd.Parameters.Add("@photo", SqlDbType.Image).Value = imagecontent;
cmd.ExecuteNonQuery(); // insert
con.Close();
TextBox1.Text = "";
cmd.Parameters.Clear();
Response.Write("Person Modified Successfully");
}
protected void Button2_Click(object sender, EventArgs e)
{
{
Stream imagestream;
int len = FileUpload1.PostedFile.ContentLength; // get length of the file
imagestream = FileUpload1.PostedFile.InputStream; // get stream for the image
Byte[] imagecontent = new Byte[len]; // create an array of bytes to hold image data
imagestream.Read(imagecontent, 0, len); // read image into array
SqlConnection con = new SqlConnection("Data Source=dev1\\sqlexpress;Initial Catalog=web;uid=write_images;password=write_images");
con.Open();
SqlCommand cmd = new SqlCommand("insert into persons values (@name,@photo,null)", con);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = TextBox1.Text;
cmd.Parameters.Add("@photo", SqlDbType.Image).Value = imagecontent;
cmd.ExecuteNonQuery(); // insert
con.Close();
TextBox1.Text = "";
cmd.Parameters.Clear();
Response.Write("Person Added Successfully");
}
}
</script>
<html xmlns=" >
<head runat="server">
<title>Modify Photo</title>
</head>
<body>
<h2>Modify Photo</h2>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Person Name
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Person's Photo
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
</table>
<p />
<asp:Button ID="Button1" runat="server" Text="Modify Photo"
OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Add Photo" Width="115px" />
<p />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="persons.aspx">Show Persons</asp:HyperLink>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" DataKeyNames="image_id" DataSourceID="SqlDataSource1"
ForeColor="Black" GridLines="Vertical" Width="547px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="image_id" HeaderText="image_id"
InsertVisible="False" ReadOnly="True" SortExpression="image_id" />
<asp:BoundField DataField="person_id" HeaderText="person_id"
SortExpression="person_id" />
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# GetUrl(Eval("name"))%>' runat="server" Width="100" Height="100"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:webConnectionString %>"
SelectCommand="SELECT * FROM [persons] ORDER BY [name], [image_id]">
</asp:SqlDataSource>
</form>
</body>
</html>
</code
<code>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.IO "%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<script runat="server">
String GetUrl(Object name)
{
return "getphoto.aspx?name=" + name.ToString();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Stream imagestream;
int len = FileUpload1.PostedFile.ContentLength; // get length of the file
imagestream = FileUpload1.PostedFile.InputStream; // get stream for the image
Byte [] imagecontent = new Byte[len]; // create an array of bytes to hold image data
imagestream.Read(imagecontent, 0, len); // read image into array
SqlConnection con = new SqlConnection("Data Source=dev1\\sqlexpress;Initial Catalog=web;uid=write_images;password=write_images");
con.Open();
SqlCommand cmd = new SqlCommand("update persons set photo =(@photo) where name=@name",con);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = TextBox1.Text;
cmd.Parameters.Add("@photo", SqlDbType.Image).Value = imagecontent;
cmd.ExecuteNonQuery(); // insert
con.Close();
TextBox1.Text = "";
cmd.Parameters.Clear();
Response.Write("Person Modified Successfully");
}
protected void Button2_Click(object sender, EventArgs e)
{
{
Stream imagestream;
int len = FileUpload1.PostedFile.ContentLength; // get length of the file
imagestream = FileUpload1.PostedFile.InputStream; // get stream for the image
Byte[] imagecontent = new Byte[len]; // create an array of bytes to hold image data
imagestream.Read(imagecontent, 0, len); // read image into array
SqlConnection con = new SqlConnection("Data Source=dev1\\sqlexpress;Initial Catalog=web;uid=write_images;password=write_images");
con.Open();
SqlCommand cmd = new SqlCommand("insert into persons values (@name,@photo,null)", con);
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = TextBox1.Text;
cmd.Parameters.Add("@photo", SqlDbType.Image).Value = imagecontent;
cmd.ExecuteNonQuery(); // insert
con.Close();
TextBox1.Text = "";
cmd.Parameters.Clear();
Response.Write("Person Added Successfully");
}
}
</script>
<html xmlns=" >
<head runat="server">
<title>Modify Photo</title>
</head>
<body>
<h2>Modify Photo</h2>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Person Name
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Person's Photo
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
</table>
<p />
<asp:Button ID="Button1" runat="server" Text="Modify Photo"
OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Add Photo" Width="115px" />
<p />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="persons.aspx">Show Persons</asp:HyperLink>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" DataKeyNames="image_id" DataSourceID="SqlDataSource1"
ForeColor="Black" GridLines="Vertical" Width="547px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="image_id" HeaderText="image_id"
InsertVisible="False" ReadOnly="True" SortExpression="image_id" />
<asp:BoundField DataField="person_id" HeaderText="person_id"
SortExpression="person_id" />
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# GetUrl(Eval("name"))%>' runat="server" Width="100" Height="100"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:webConnectionString %>"
SelectCommand="SELECT * FROM [persons] ORDER BY [name], [image_id]">
</asp:SqlDataSource>
</form>
</body>
</html>
</code