Nate_LR
asked on
Trouble with Update in FormView in ASP.net in C-sharp
I'm pretty new at asp.net in c-sharp and I cannot whatsoever figure out why the Update statement won't work on this page....
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="DisplayPhoto.aspx.cs" Inherits="DisplayPhoto" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
 <asp:HyperLink ID="HyperLink1" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="14px">Back to the album</asp:HyperLink>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" Width="100%" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Bind("relPath", "FieldPicsN/{0}") %>' />
</td>
<td>
<table>
<tr>
<td>
Photographer:
</td>
<td>
<asp:TextBox ID="PhotoByTxt" runat="server" Text='<%# Bind("photoBy") %>' Width="150px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<asp:TextBox ID="DescriptionTxt" runat="server" Text='<%# Bind("photoDesc") %>' Width="150px" Rows="10" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Public Photo?:
</td>
<td>
<asp:DropDownList ID="PublicList" runat="server" SelectedValue='<%# Bind("photoPublic") %>' AppendDataBoundItems="true">
<asp:ListItem Value="Y" Text="Y"></asp:ListItem>
<asp:ListItem Value="N" Text="N"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" ForeColor="White">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" ForeColor="White">
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<%--<asp:Button ID="btnRotLeft" runat="server" Text="Rotate Left" />
<asp:Button ID="btnRotRight" runat="server" Text="Rotate Right" OnClick="btnRotRight_Click" />--%>
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Bind("relPath", "FieldPicsN/{0}") %>' />
<%--<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Bind("fullPath") %>' />--%>
</td>
<td>
<table>
<tr>
<td>
Photographer:
</td>
<td>
<asp:Label ID="PhotoByLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoBy") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoDesc") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Public Photo?:
</td>
<td>
<asp:Label ID="PublicLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoPublic") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnRotLeft" runat="server" Text="Rotate Left" />
<asp:Button ID="btnRotRight" runat="server" Text="Rotate Right" OnClick="btnRotRight_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:Label ID="lblError" runat="server" ForeColor="Red" EnableViewState="False"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ConflictDetection="CompareAllValues"
SelectCommand="SELECT replace(relPath,'\', '/') as relPath, photoBy, photoDesc, photoPublic, fTrackID FROM fieldPics WHERE id = @PhotoID"
UpdateCommand="UPDATE fieldPics SET photoBy = @photoBy, photoDesc = @photoDesc, photoPublic = @photoPublic WHERE id = @PhotoID">
<SelectParameters>
<asp:QueryStringParameter Name="PhotoID" QueryStringField="photoid" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="photoBy" Type="String" />
<asp:Parameter Name="photoDesc" Type="String" />
<asp:Parameter Name="photoPublic" Type="String" />
<asp:QueryStringParameter Name="PhotoID" QueryStringField="photoid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Threading;
public partial class DisplayPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlParameter[] p = new SqlParameter[1];
p[0] = new SqlParameter("@photoid", Request.QueryString["photoid"]);
int trackid = (int)SqlHelper.ExecuteScalar("select fTrackID from fieldPics where id = @photoid", p);
HyperLink1.NavigateUrl = "~/DisplayTrack.aspx?trackid=" + trackid.ToString();
}
}
protected void btnRotRight_Click(object sender, EventArgs e)
{
Image img = (Image)FormView1.FindControl("Image1");
string arg = @"convert """ + Server.MapPath(img.ImageUrl) + @""" -rotate 90 """ + Server.MapPath(img.ImageUrl) + @"""";
callImgMagick(arg);
displayImg(img.ImageUrl, 5000);
}
protected void callImgMagick(string fileargs)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.Arguments = fileargs;
proc.StartInfo.FileName = Server.MapPath("MagickCMD.exe");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
}
protected void displayImg(string imgSrc, int delay)
{
Thread.Sleep(delay);
Image img = (Image)FormView1.FindControl("Image1");
img.ImageUrl = imgSrc;
}
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = "A database error has occurred.<br /><br />" +
"Message: " + e.Exception.Message;
e.ExceptionHandled = true;
e.KeepInEditMode = true;
}
else if (e.AffectedRows == 0)
{
lblError.Text = "Another user may have updated this photo." +
"<br />Please try again.";
}
else
{
FormView1.DataBind();
}
}
}
ASKER
I do not get an error message. When I click Update I want to, of course, update columns photoBy, photoDesc, and photoPublic in the fieldPics table in my SQL Server database. If I attempt to do this on a row where photoDesc is NULL, then that column will update. If I attempt the update on a row where none of the three columns are NULL, then no column will update.
I apologize not being clear before. Sometimes you've spent so much time trying to figure out the problem yourself that you get impatient and negligent when asking for help. I'm the only programmer here so I hope the 'experts' can help.
I apologize not being clear before. Sometimes you've spent so much time trying to figure out the problem yourself that you get impatient and negligent when asking for help. I'm the only programmer here so I hope the 'experts' can help.
I don't see where the update is supposed to happen.
I have a few ideas about reason:
1) The button click doesn't fire.
2) The update statement has a where condition that is not met.
3) The update is writing to a different database than the one you are checking.
I have a few ideas about reason:
1) The button click doesn't fire.
2) The update statement has a where condition that is not met.
3) The update is writing to a different database than the one you are checking.
ASKER
Bob, there is an UpdateCommand in the SqlDataSource and in the EditItemTemplate is an Update Button.
1. The button click does fire, because if any of the three columns I'm attempting to update are NULL then it gets updated, but not so otherwise.
2. The where condition is being met because the correct row gets updated, that is if a column is NULL.
3. There is no way I'm writing to a different database.
I've been fiddling with this with not luck. I noticed my code above isn't formatted correctly so I'll provide it again. If I try to update where are three columns aren't NULL then I do get the message...
"Another user may have updated this photo.
Please try again"
Resulting from....
else if (e.AffectedRows == 0)
1. The button click does fire, because if any of the three columns I'm attempting to update are NULL then it gets updated, but not so otherwise.
2. The where condition is being met because the correct row gets updated, that is if a column is NULL.
3. There is no way I'm writing to a different database.
I've been fiddling with this with not luck. I noticed my code above isn't formatted correctly so I'll provide it again. If I try to update where are three columns aren't NULL then I do get the message...
"Another user may have updated this photo.
Please try again"
Resulting from....
else if (e.AffectedRows == 0)
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="DisplayPhoto.aspx.cs" Inherits="DisplayPhoto" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:HyperLink ID="HyperLink1" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="14px">Back to the album</asp:HyperLink>
<%--<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound"
OnItemCreated="FormView1_ItemCreated" Width="100%" >--%>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" Width="100%" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<table style="width: 100%">
<tr>
<td>
<%--<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Eval("relPath", "FieldPics/{0}") %>' />--%>
<%--<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Bind("fullPath") %>' />--%>
</td>
<td>
<table>
<tr>
<td>
PhotoID:
</td>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Text='<%# Bind("id") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Photographer:
</td>
<td>
<asp:TextBox ID="PhotoByTxt" runat="server" Text='<%# Bind("photoBy") %>' Width="150px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<asp:TextBox ID="DescriptionTxt" runat="server" Text='<%# Bind("photoDesc") %>' Width="150px" Rows="10" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Public Photo?:
</td>
<td>
<%--<asp:TextBox ID="PublicTxt" runat="server" Text='<%# Bind("photoPublic") %>'></asp:TextBox>--%>
<asp:DropDownList ID="PublicList" runat="server" SelectedValue='<%# Bind("photoPublic") %>' AppendDataBoundItems="true">
<asp:ListItem Value="Y" Text="Y"></asp:ListItem>
<asp:ListItem Value="N" Text="N"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="False" CommandName="Update"
Text="Update" ForeColor="White">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" ForeColor="White">
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<%--<asp:Button ID="btnRotLeft" runat="server" Text="Rotate Left" />
<asp:Button ID="btnRotRight" runat="server" Text="Rotate Right" OnClick="btnRotRight_Click" />--%>
</td>
</tr>
</table>
</EditItemTemplate>
<ItemTemplate>
<table style="width: 100%">
<tr>
<td>
<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Eval("relPath", "FieldPics/{0}") %>' />
<%--<asp:Image ID="Image1" runat="server" Width="1000px" ImageUrl='<%# Bind("fullPath") %>' />--%>
</td>
<td>
<table>
<tr>
<td>
PhotoID:
</td>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Text='<%# Bind("id") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Photographer:
</td>
<td>
<asp:Label ID="PhotoByLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoBy") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoDesc") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td>
Public Photo?:
</td>
<td>
<asp:Label ID="PublicLabel" runat="server" Font-Bold="True" Text='<%# Bind("photoPublic") %>' Font-Names="Verdana" Font-Size="12px"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnRotLeft" runat="server" Text="Rotate Left" />
<asp:Button ID="btnRotRight" runat="server" Text="Rotate Right" OnClick="btnRotRight_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:Label ID="lblError" runat="server" ForeColor="Red" EnableViewState="False"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT replace(relPath,'\', '/') as relPath, replace(fullPath,'\', '/') as fullPath, photoBy, photoDesc, photoPublic, fTrackID, id FROM fieldPics WHERE id = @PhotoID"
UpdateCommand="UPDATE fieldPics SET
photoBy = @photoBy,
photoDesc = @photoDesc,
photoPublic = @photoPublic
WHERE id = @PhotoID
AND photoBy = @orig_photoBy
AND photoDesc = @orig_photoDesc
AND photoPublic = @orig_photoPublic">
<SelectParameters>
<asp:QueryStringParameter Name="PhotoID" QueryStringField="photoid" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="photoBy" Type="String" />
<asp:Parameter Name="photoDesc" Type="String" />
<asp:Parameter Name="photoPublic" Type="String" />
<asp:Parameter Name="PhotoID" Type="Int32" />
<%--<asp:QueryStringParameter Name="PhotoID" QueryStringField="photoid" Type="Int32" />--%>
<asp:Parameter Name="orig_photoBy" Type="String" />
<asp:Parameter Name="orig_photoDesc" Type="String" />
<asp:Parameter Name="orig_photoPublic" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = "A database error has occurred.<br /><br />" +
"Message: " + e.Exception.Message;
e.ExceptionHandled = true;
e.KeepInEditMode = true;
}
else if (e.AffectedRows == 0)
{
lblError.Text = "Another user may have updated this photo." +
"<br />Please try again.";
}
else
{
FormView1.DataBind();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oddly, it makes no difference with the select command....
SelectCommand="SELECT replace(relPath,'\', '/') as relPath, replace(fullPath,'\', '/') as fullPath, photoBy, photoDesc, photoPublic, fTrackID, id FROM fieldPics WHERE id = @PhotoID"
<SelectParameters>
<asp:QueryStringParameter Name="PhotoID" QueryStringField="photoid" Type="Int32" />
</SelectParameters>
ASKER
This was an issue as well...
ConflictDetection="Compare AllValues"
ConflictDetection="Compare
ASKER
Because I figured it out on my own.
The first thing you need to understand, even before you figure out the UPDATE statement, is that we do not have a crystal ball and we certainly are not sitting at your workstation. So please tell us what you mean by "won't work". Do you mean:
if this does not make any sense, then consider the last time your took your car/bicycle to the shop. We can only assume that you did not tell the mechanic that your car/bicycle "won't work". Then consider, that we do not even have your car...