I would like to insert a database entry by using standard functions of my GridView and SqlDataSource. Has there been any update on this topic since the dirty workarounds some versions ago (e.g. see here, here)? I could not find any new tutorials on the web.
For example, is there any handler that allows the entry to be written to the database by using the standard -Datafield and the UpdateCommand of the SqlDataSource? At the moment, the UpdateCommand does not seem to get my entered value.
How can I debug those SqlDataSource-Database-Queries properly?
Heres the solution I've tried to get it working with: My database table tblname contains only one column, "name".
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>"
DeleteCommand="DELETE FROM [tblname] WHERE [name] = #name"
InsertCommand="INSERT INTO [tblname] ([name]) VALUES (#name)"
SelectCommand="SELECT [name] FROM [tblname]"
UpdateCommand="UPDATE [tblname] SET [name] = #name WHERE [name] = #name">
<DeleteParameters><asp:Parameter Name="name" Type="String" /></DeleteParameters>
<InsertParameters><asp:Parameter Name="name" Type="String" /></InsertParameters>
<UpdateParameters><asp:Parameter Name="name" Type="String" /></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="name" DataSourceID="SqlDataSource1" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Name" ShowHeader="True">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Insertname" name="name_new" runat="server" Text='<%# Bind("name") %>' />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="True">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button4" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Sure?');" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Edit entry" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button5" runat="server" Text="New" CommandName="Insert" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Related
It is interesting that the asp:Label in ItemTemplate converts the following value userId=%UserId%¶m=%param% into userId=%UserId%¶m=%param%. It converts ¶ into ¶.
The aspx code looks something like this, it uses asp:ObjectDataSource to get the data to bind:
<asp:DetailsView ID="dvLink" runat="server" DataSourceID="dsLink" AutoGenerateRows="False" DefaultMode="Insert" DataKeyNames="LinkId" OnModeChanged="dvLink_ModeChanged">
<Fields>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Parameters" SortExpression="Parameters">
<ItemTemplate>
<asp:Label ID="lblParameters" runat="server" Text='<%# Bind("Parameters") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbParameters" runat="server" Text='<%# Bind("Parameters") %>' Width="500px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="tbParameters" runat="server" Text='<%# Bind("Parameters") %>' Width="500px"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="dsLink" runat="server" SelectMethod="GetLink"
TypeName="LinkLib" InsertMethod="SaveLink"
OnInserted="dsLink_Inserted" OnUpdated="dsLink_Updated"
UpdateMethod="SaveLink" DataObjectTypeName="LinkLib">
<SelectParameters>
<asp:ControlParameter ControlID="gvLinks" Name="LinkId" PropertyName="SelectedValue"
Type="Int32" />
<asp:Parameter Name="auth" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
How can I escape ¶ during bind so it doesn't convert it into html code?
Thanks to #Greg for pointing out to a right SO article, that solved the issue. Here is the solution, if someone has a similar issue:
I changed <asp:label> to <asp:Literal> to fix the issue. Also, make sure to use the following attribute Mode="Encode".
I replaced this aspx code
<ItemTemplate>
<asp:Label ID="lblParameters" runat="server" Text='<%# Bind("Parameters") %>'></asp:Label>
</ItemTemplate>
to
<ItemTemplate>
<asp:Literal ID="lblParameters" runat="server" Mode="Encode" Text='<%# Bind("Parameters") %>'></asp:Literal>
</ItemTemplate>
for the past days i've been trying to find a solution for my problem but so far i couldn't.
I Have a Gridview that in edit mode, one of the fields have a DropDownList of people that is populated by a Select that uses two other fields of that row (State and City). I tried everything i found on the web about it and i couldn't make it work. Can you help me please?
Code below
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
...stuff...
<asp:TemplateField HeaderText="UF" SortExpression="UF">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People" SortExpression="People">
<ItemTemplate>
<asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People"
DataValueField="People">
</asp:DropDownList>
<asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
<asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
<asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server"
ConnectionString="<%$ ConnectionStrings:dbdb %>"
ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
SelectCommand="SELECT People FROM Table_People WHERE (Cidade = #Municipio) AND (UF = #UF)"
CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="Hidden2" Name="UF" PropertyName="Value" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
...stuff...
</asp:GridView>
What am i doing wrong?
UPDATE
when i click edit, the dropdownlist does not load values.
And if i set the property CancelSelectOnNullParameter="false" on SqlDataSource it shows 'Must declare scalar variable #xxx'
I have inspected the hiddenfields and they have values loaded, so the problem is the controlparameter taht does not get the value from it
What you should do is have the hidden fields and the sqldatasource before the dropdownlist. The issue you are experiencing is due to when the data is loaded.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
DataKeyNames="id,Municipio,UF" DataSourceID="SqlDataSource5">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
...stuff...
<asp:TemplateField HeaderText="UF" SortExpression="UF">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UF") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UF") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Municipio" SortExpression="Municipio">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Municipio") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Municipio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="People" SortExpression="People">
<ItemTemplate>
<asp:Label ID="LbPeople" runat="server" Text='<%# Bind("Cultura") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="Hidden1" Value='<%# Bind("Municipio") %>' runat="server"/>
<asp:HiddenField ID="Hidden2" Value='<%# Bind("UF") %>' runat="server"/>
<asp:SqlDataSource ID="SqlDataSourcePeopleEdit" runat="server"
ConnectionString="<%$ ConnectionStrings:dbdb %>"
ProviderName="<%$ ConnectionStrings:somar.ProviderName %>"
SelectCommand="SELECT People FROM Table_People WHERE (Cidade = #Municipio) AND (UF = #UF)"
CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:ControlParameter ControlID="Hidden1" Name="Municipio" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="Hidden2" Name="UF" PropertyName="Value" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="DdlPeople" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourcePeopleEdit" DataTextField="People"
DataValueField="People">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
...stuff...
</asp:GridView>
I have two itemTemplate in Gridview, need to perform multiplication operation and store it in another column, How would i do that? i.e multiply productPrice and productQuantity and store in totalPrice label. Thank You
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AutopartConnectionString %>"
SelectCommand="SELECT [id], [productID], [productName], [productPrice], [productQuantity], [totalPrice] FROM [cartDetails]"
UpdateCommand="UPDATE [cartDetails] SET [productQuantity]=#productQuantity WHERE [productID]=#productID"
DeleteCommand="DELETE [cartDetails] WHERE [productID]=#productID">
<%--<UpdateParameters>
<asp:Parameter Name="productQuantity" Type="Int32" />
<asp:Parameter Name="productQuantity" Type="String" />
</UpdateParameters>--%>
<DeleteParameters>
<asp:Parameter Name="productID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productID" SortExpression="productID" >
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("productID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productName" SortExpression="productName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("productName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productPrice" SortExpression="productPrice">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("productPrice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productQuantity" SortExpression="productQuantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("productQuantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("productQuantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="totalPrice" SortExpression="totalPrice">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# ((Convert.ToDecimal(Eval("productPrice")))*(Convert.ToDecimal(Eval("productQuantity")))).ToString() %>'></asp:Label>
<%-- <asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'></asp:Label>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
My Table structure
Create table cartDetails(
id int not null identity(1,1) primary key,
productID int not null,
productName VARCHAR(150) not null ,
productPrice float,
productQuantity int,
totalPrice float
);
Is there any reason for not doing the math in the Select? or you can even make totalPrice a computed field in the table designer.
SELECT [id]
, [productID]
, [productName]
, [productPrice]
, [productQuantity]
, [productPrice] * [productQuantity] as [totalPrice]
FROM [cartDetails]
and then just bind totalPrice
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'</asp:Label>
</ItemTemplate>
Also, regarding math within an ItemTemplate; you must use Eval() and you must make sure there are no null values. Judicious use of isnull(<field>,0) in the SELECT statement as needed.
I have multiple gridviews on one page. For each gridview, there are two ways to insert a new record. One is through FooterTemplate of a TemplateField when the gridview has data and the other through EmptyDataTemplate when the gridview is empty.
By itself, the inserts work well. However, if another gridview is empty, then by default, the insert portion in its EmptyDataTemplate is displayed. When this is displayed, I can't get the FooterTemplate of another gridview to display. That portion is inaccessible until I get rid of the EmptyDataTemplate by forcing an insert.
<asp:Button ID="btnAddNewDomSoilType" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewDomSoilType" />
<asp:GridView ID="gvDomSoilType" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsDomSoilType" CssClass="data"
AllowSorting="True" onrowcommand="gvDomSoilType_RowCommand" DataKeyNames="HabitatObsSubstrateID, HabitatObservationID, SubstrateID" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Substrate" SortExpression="Substrate">
<FooterTemplate>
<asp:DropDownList ID="ddlSoilType" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Substrate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label4" runat="server" Text="Substrate: " CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlSoilType2" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID="btnAddNewVegCover" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewVegCover" />
<asp:GridView ID="gvVegCover" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsVegCover" CssClass="data"
AllowSorting="True" onrowcommand="gvVegCover_RowCommand" DataKeyNames="HabitatObsVegID">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HabitatVegetationType" SortExpression="HabitatVegetationType">
<FooterTemplate>
<asp:DropDownList ID="ddlVegType" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegType" runat="server" Text='<%# Bind("HabitatVegetationType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VegetationTypePercent" SortExpression="VegetationTypePercent">
<FooterTemplate>
<asp:TextBox ID="txtVegTypePercent" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegTypePercentage" runat="server" Text='<%# Bind("VegetationTypePercent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label2" runat="server" Text="Vegetation Type:" CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlVegType2" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label5" runat="server" Text="Percenttage:" CssClass="insertLabel"></asp:Label>
<asp:TextBox ID="txtVegTypePercent2" runat="server"></asp:TextBox>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
Code behind:
protected void addNewDomSoilType(object sender, EventArgs e)
{
gvDomSoilType.ShowFooter = true;
}
protected void addNewVegCover(object sender, EventArgs e)
{
gvVegCover.ShowFooter = true;
}
I know my explanation is a bit confusing. Let me know what doesn't make sense and I'll try to clarify. Thanks for looking.
These two gridviews shouldn't impact each other. I think it might be the PostBack that is messing up the state of the webpage. On the PageLoad do Page.IsPostBack false and then populate the data. Make sure the events are hooked up to the right GridView.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/" + Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
My Image field is saved in (SQL server 2005)...and image inserted in database through file upload button ...
and now i want to show data in gridview but not display image field and other field shown in gridview very well becauese other fields are text format.
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
Your Problem was that string.Format accepts arguments and you were using a concat operation. Here i have used {0} - stands for argument at/for index 0. Similarly for multiple arguments you can use string.Format("{0} {1} {2}",var1,var2,var3).
Also above i am assuming {0} will be replaced by file name with extension , like abc.png.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="bookname" Height="504px"
Width="289px">
<Columns>
<asp:TemplateField HeaderText="image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# String.Format("~/path/to/image/{0}" , Eval("image")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("image") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True"
SortExpression="bookname" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
</asp:SqlDataSource>
I would expect rather something like
ImageUrl='<%# this.ResolveUrl("~/path/to/image/" + Eval("image")) %>' />
rather than string.Format
As your image is stored into database, you need to use a handler to retrieve the image. You can get a referance here http://www.codeproject.com/Articles/271590/Show-an-image-saved-in-a-SQL-Table-on-an-ASP-Net-I