Arash
Arash

Reputation: 1826

Connection string between asp.net and SQL Server

I wrote some code for getting a connection between asp.net and SQL Server but I have

Generally, the purpose of this code is to add 2 sets of data into the database using 2 text boxes.

This error is in web.config file:

Error 1 Character ';', hexadecimal value 0x3b is illegal in an XML name.

My configuration:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI"; providerName="System.Data.SqlClient" />
  </connectionStrings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
</configuration>

In code behind :

The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)

My code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("INSERTINTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

best regards :)

=========================================

EDIT: I had done according your suggestions and there is no more error but the data isn't added to the database, actually nothing happens when i click on submit. :(

my code:

web.config:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

code behind :

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

namespace WebApplication3
{

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (name,fathername) VALUES('" + txt1.Text + "','" + txt2.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();

            txt1.Text = "";
            txt2.Text = "";

            con.Close();
        }
    }
}

Upvotes: 1

Views: 2027

Answers (4)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Error1: You have added semicolon at the end of the connection string asbelow:

connectionString="data source=.\SQLEXPRESS;initial catalogue=test;
       Integrated Security=SSPI";
                            ^^^^^

Solution1: You need to move the semicolon inside connectionstring

Try This:

connectionString="data source=.\SQLEXPRESS;initial catalogue=test;
                                         Integrated Security=SSPI;"

Error 2: You did not import the System.Data.SqlClient namespace to use the SqlConnection class members

Solution2:

You need to import System.Data.SqlClient; Namespace asbelow

using System.Data.SqlClient;

Solution 3: initial catalogue spelling is wrong, instead you should use Initial Catalog in your Connection string asbelow:

connectionString="data source=.\SQLEXPRESS;initial catalog=test; Integrated Security=SSPI";

EDIT: Try the following code to get the exceptiondetails

protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
        SqlConnection con = new SqlConnection(cs);

       try
       {
        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;

        SqlCommand cmd = new SqlCommand("INSERT INTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();           
       }
    catch(Exception ex)
    {
    String ErrorMsg=ex.ToString();
    }
   finally
   {
       con.Close();
   }
    }

Upvotes: 2

sudhakarssd
sudhakarssd

Reputation: 451

Error says clearly! Remove semicolon(;) from your connection string in configuration tag. As shown below

  <connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>

Also you missed the namespace,

using System.Data.SqlClient

Upvotes: 0

marc_s
marc_s

Reputation: 755207

I think you should have this configuration

<connectionStrings>
   <add name="myconectionstring"
        connectionString="data source=.\SQLEXPRESS;initial catalog=test;Integrated Security=SSPI;"
        providerName="System.Data.SqlClient" />
</connectionStrings>

or

<connectionStrings>
   <add name="myconectionstring"
        connectionString="server=.\SQLEXPRESS;database=test;Integrated Security=SSPI;" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

Use Initial Catalog or database instead of your initial catalogue= (or use server= and database= which are clearer and more obvious names, in my opinion)

This website http://www.connectionstrings.com/ is a great and extensive resource about how to build valid connection strings.

And of course, as all the others have mentioned - you need to add a using System.Data.SqlClient to your code file!

Upvotes: 0

detaylor
detaylor

Reputation: 7280

You have a couple of errors. The first is that you have placed the semi-colon (;) outside the connectionString attribute value in the configuration it should be:

<connectionStrings>
    <add name="myconectionstring"
    connectionString="data source=.\SQLEXPRESS;initial catalogue=test;Integrated Security=SSPI;" 
    providerName="System.Data.SqlClient" />
  </connectionStrings>

In your code file you have not imported the required namespace try adding:

using System.Data.SqlClient

Upvotes: 0

Related Questions