I saved the image into the database in binary format. I get an error when i am trying to retrieve it. The error says "The connection name 'ConnectionString' was not found in the applications configuration or the connection string is empty." This is my code, what am i doing wrong??
//Code for generic handler
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Data Source=ACER-
PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated
Security=True"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ImageName,Image from image" + " where ID =@ID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Image"]);
dReader.Close();
con.Close();
}
//Code for datagrid
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [ImageName], [Image]
FROM [image]"></asp:SqlDataSource>
"Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" is your Connection String.
Check your web.config and see if you have something like this:
<connectionStrings>
<add name="myConnection" connectionString="Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
After that, change your line to
con.ConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
Wrong:
con.ConnectionString = ConfigurationManager.ConnectionStrings[ "Data Source=ACER-
PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" ].ConnectionString;
It should be..
con.ConnectionString = "Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True";
Or
con.ConnectionString = ConfigurationManager.ConnectionStrings["YourConnection"].ConnectionString;
So, you need to put following in your app.config or web.config
<connectionStrings>
<add name="YourConnection" connectionString="Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=imageDemo;Integrated Security=True" providerName="System.Data.SqlClient"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.