0

I have this error when I try to insert an image into a table located in my database from my webpage using this script:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class editorlogin : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //save image into database

            string str = FileUpload1.FileName;
            FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//uploads//"+ str);
            string path = "~/uploads/" + str.ToString();
            con.Open();
            SqlCommand cmd=new SqlCommand("Insert into upload values('" + TextBox1.Text + "','" + path + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label1.Text = "Image Uploaded Successfully";


        }
        else
        {
            Label1.Text = "Please, Upload your image";
        }
    }
}

This is the error i get The "ConnectionString property has not been initialized."

Would be greatful for any help, thank-you.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
  • Where is your connection string? – Anthony Horne May 13 '14 at 07:17
  • 1
    This question doesn't even deserve any answer since can be found a lot of duplicates over Stack Overflow or using Google. – Soner Gönül May 13 '14 at 07:19
  • 1
    @SonerGönül I couldn't find one. Most questions refer to values not being pulled in from Web.config/data source and I'm sure if OP didn't realise they *needed* a connection string, this information would have been overwhelming. Just because the error is frequent, doesn't mean the root cause is the same. – CodingIntrigue May 13 '14 at 07:49
  • Possible duplicate [How to fix “The ConnectionString property has not been initialized”](http://stackoverflow.com/questions/1007786/how-to-fix-the-connectionstring-property-has-not-been-initialized) – Soner Gönül May 13 '14 at 07:53
  • @RGraham I found one. I don't like this kind of questions that they don't fit here. I vote to close immediately. Because soultion can be found in 30 seconds using Google or reading their documentations. Error message is clear; **ConnectionString property has not been initialized** That means OP should check it's connection string first. **OH WAIT!** He doesn't even have one. – Soner Gönül May 13 '14 at 08:00
  • 1
    @SonerGönül That's not the same issue in the OPs eyes - and I can see why. The OP doesn't have the knowledge to be able to decipher that the issue in that link question applies to him because he has no knowledge of Web.config/connection string. It's unfair to deny him the answer for his own localized purposes. Like I said, the error is the same, the cause is not. – CodingIntrigue May 13 '14 at 08:07

4 Answers4

3

The error is self-explanatory, you haven't initialized the connection-string here:

SqlConnection con = new SqlConnection("");

Where is the connection string?

You need something like:

SqlConnection con = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)");

Apart from that you're open for sql-injection here:

"Insert into upload values('" + TextBox1.Text + "','" + path + "')"

Don't concenate strings to build your sql query but use parameterized queries.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Apologies, I am very much a beginner to this. I have obviously missed it but how do I go about implementing the connection string? – Owen Farnan May 13 '14 at 07:20
  • con.ConnectionString = ConfigurationSettings.AppSettings["D:\Users\Owen\Desktop\WebsiteFinal\App_Data"]; Would this be of help? – Owen Farnan May 13 '14 at 07:23
  • @OwenFarnan: have a look [here](http://www.connectionstrings.com/) for valid connection strings. I don't know your database, server and authorization (and i don't want to know). **Edit** No, you need a real connection string, maybe: `conn.ConnectionString=ConfigurationSettings.AppSettings["databasepath"]` if your configuration file has a setting `databasepath`(you have to add it). – Tim Schmelter May 13 '14 at 07:23
  • You can find the connectionString you need here: http://www.connectionstrings.com/ @OwenFarnan – DatRid May 13 '14 at 07:24
  • 1
    +1 connection string,sql injection;) – Nagaraj S May 13 '14 at 07:24
2

In this line:

SqlConnection con = new SqlConnection("");

You don't provide a value to tell the application which SQL server to connect to. You need to populate this with something. You can find a list of appropriate connection strings at:

http://www.connectionstrings.com/sql-server/

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
1

Your SqlConnection ConnectionString property has now value. you pass it "" in the constructor. You should use something like the following:

"data source=YOUR_SERVER_NAME\SERVER_INSTANCE;initial catalog=DB_NAME;user=USER_NAME;password=PASSWORD"
शेखर
  • 17,412
  • 13
  • 61
  • 117
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43
1

Error is very basic and descriptive, you should not keep your connectionclass constructor as 'empty string' you need to provide connection string in it see below snippet

//Wrong
SqlConnection con = new SqlConnection("");

//Right
string szConnectStr = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;";
SqlConnection con = new SqlConnection(szConnectStr);
con.Open
{
   //code here
}
con.Close();

hope it helps

koolprasad2003
  • 299
  • 3
  • 23