0

i wrote a connection string in asp.net i.e add 2 data through 2 textboxes into the sql server database . but after pressing submit button i face to an error .

Details:

web.config:

<!--
  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;
using System.Collections.Generic;
using System.Linq;
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_Click1(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 Table_1 (name,fathername) VALUES('" + txt1.Text + "','" + txt2.Text + "')", con);
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                String ErrorMsg = ex.ToString();
            }
            finally
            {
                con.Close();
            }
        }
    }
}

Error message :

Server Error in '/' Application.

Keyword not supported: 'initial catalogue'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Keyword not supported: 'initial catalogue'.

Source Error: 


Line 21:         {
Line 22:             string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
Line 23:             SqlConnection con = new SqlConnection(cs);
Line 24: 
Line 25:             try

Source File: c:\Users\Admin\Documents\Visual Studio 2012\Projects\WebApplication3\WebApplication3\WebForm1.aspx.cs    Line: 23 
4
  • 1
    use Initial Catalog instead of Initial Catalogue Commented Jun 25, 2014 at 9:19
  • txt1.Text = "Robert'); DROP TABLE Table_1;--" fits nicely Commented Jun 25, 2014 at 9:23
  • Simple typographical error - should be closed. Commented Jun 25, 2014 at 9:24
  • Side note: It is recommended to use Parameterized Queries. As you are not using SqlCommand.Parameters your code clearly exposed to sql injections. Commented Jun 25, 2014 at 9:25

4 Answers 4

1

Your connection string should start as follows:

Server=myServerAddress;Database=myDataBase;

instead of your current

data source=.\SQLEXPRESS;initial catalogue=test;

This should resolve your issue.

Sign up to request clarification or add additional context in comments.

Comments

1

Use:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

And whenever you forgot a connection string of any database, go to:

http://www.connectionstrings.com

Comments

0

You misspelled Catalog wrong. Its Initial Catalog

Comments

0

its a type at Initial Catalog correct the spelling and it should work !!

for more information :-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.