0

net to adding database. I am trying to do texts on two textbox and one selected value in dropdownlist to add my table. Here is my code

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;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connectionString = @" Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\CEM\DOCUMENTS\VISUAL STUDIO 2010\WEBSITES\EKLEMEDENE\APP_DATA\DATABASE.MDF;Integrated Security=True;User Instance=True";
        string queryString = "INSERT INTO ekle(flight, name, food) VALUES   ('" + TextBox1.Text + " ' , '" + TextBox2.Text + " ' ,  '" + DropDownList1.SelectedValue + " '  )";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(queryString, con);
        con.Open();
        command.ExecuteNonQuery();

        con.Close();
    }
}

After I execute I will have error

Database 'C:\Users\Cem\Documents\Visual Studio 2010\WebSites\eklemedene\App_Data\Database.mdf' already exists. Choose a different database name. An attempt to attach an auto-named database for file C:\USERS\CEM\DOCUMENTS\VISUAL STUDIO 2010\WEBSITES\EKLEMEDENE\APP_DATA\DATABASE.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

2
  • 1
    Your connection string is wrong. You are trying to attach a database that already exists. You need to point to you database server that hosts the attached database. Try using something like developerfusion.com/tools/sql-connection-string to generate your connection string Commented May 14, 2012 at 15:14
  • Or find your appropriate connection string at connectionstrings.com Commented May 14, 2012 at 15:20

1 Answer 1

1
  1. You're wide open for SQL-Injection. Avoid passing parameters directly from controls. Instead use Parameters.
  2. Use using-statement for anything implementing IDisposable like Connections or Commands:
  3. There's something wrong with your ConnectionString, you could try to use SqlConnectionStringBuilder class:

//Build the connection 
SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();

//Put your server or server\instance name here.  Likely YourComputerName\SQLExpress
bldr.DataSource = ".\\SQLEXPRESS";

//Attach DB Filename
bldr.AttachDBFilename = @"C:\USERS\CEM\DOCUMENTS\VISUAL STUDIO 2010\WEBSITES\EKLEMEDENE\APP_DATA\DATABASE.MDF";

//User Instance
bldr.UserInstance = true;

//Whether or not a password is required.
bldr.IntegratedSecurity = true;

using(var connection = new SqlConnection(bldr.ConnectionString))
{
    var sql = "INSERT INTO ekle(flight, name, food) VALUES (@flight, @name , @food)";
    using(var command = new SqlCommand(sql, connection))
    {
        command.Parameters.AddWithValue("@flight", TextBox1.Text);
        command.Parameters.AddWithValue("@name", TextBox2.Text);
        command.Parameters.AddWithValue("@food", DropDownList1.SelectedValue); 
        connection.Open();
        command.ExecuteNonQuery();
    }
} // closes the connection implicitely
Sign up to request clarification or add additional context in comments.

2 Comments

With your answer I will have the name con does not exist in the current context and where can I see my server/instance name
@cem: I've written that without IDE, that was just a typo. So replace using(var command = new SqlCommand(sql, con)) with using(var command = new SqlCommand(sql, connection)). Edited my answer accordingly.

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.