Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have error inthis code that error is(The connection name 'ConnectionString' was not found in the applications configuration or the connection string is empty.)
XML
Line 48: </Columns>
Line 49: </asp:GridView>
Line 50: <asp:SqlDataSource ID="SqlDataSource1" runat="server"
Line 51: ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
Line 52: SelectCommand="SELECT [ID], [ImageName], [Image]




(but am using sql local server windows authenticatation in that there is no userid and passwor)


C#
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class fileupload : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strImageName = txtName.Text.ToString();
        if (FileUpload1.PostedFile != null &&
            FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte
                          [FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read
               (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

            // Create SQL Connection 
            SqlConnection con = new SqlConnection("server=USER\\SQLEXPRESS;user id=;password;database=images");
            con.ConnectionString = ConfigurationManager.ConnectionStrings
                                   ["ConnectionString"].ConnectionString;

            // Create SQL Command 

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO Images(ImageName,Image)" +
                              " VALUES (@ImageName,@Image)";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            SqlParameter ImageName = new SqlParameter
                                ("@ImageName", SqlDbType.VarChar, 50);
            ImageName.Value = strImageName.ToString();
            cmd.Parameters.Add(ImageName);

            SqlParameter UploadedImage = new SqlParameter
                          ("@Image", SqlDbType.Image, imageSize.Length);
            UploadedImage.Value = imageSize;
            cmd.Parameters.Add(UploadedImage);
            con.Open();
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result > 0)
                lblMessage.Text = "File Uploaded";
            GridView1.DataBind();


        }
    }
}
<%@ 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("server=USER\\SQLEXPRESS;user id=;password;database=images");
        con.ConnectionString = ConfigurationManager.ConnectionStrings
                              ["ConnectionString"].ConnectionString;
        // Create SQL Command 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ImageName,Image from Images" + 
                  " 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();
}


    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
Posted
Updated 2-Feb-12 19:51pm
v2

Write This Code IN web.config file

HTML
<connectionstrings>
    <add name="ConnectionString">
      connectionString='your connection string' />
  </connectionstrings>
 
Share this answer
 
v2
I guess some thing is wrong with your connection string kindly check following link
http://msdn.microsoft.com/en-us/library/ff647396.aspx[^]
 
Share this answer
 
v2
if your dont have key under ConnectionString in web.config then add as
XML
<connectionstrings>
    <add name="ConnectionString">
      connectionString='your connection string' />
  </connectionstrings>
 
Share this answer
 
Does using the same name as ConnectionString is the problem here?
["ConnectionString"]

Also check if you have two connection string with same name?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900