Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an app which should insert text input into a sql database. I've attempted two different methods, each on a different button event. Using Visual Studio 2008, I tried placing the mdf db in the app folder and also in the debug folder, changing the connection string. Can anybody please tell me why this code will insert data to a dataset variable but not into the database.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace TextAdd3
{
    

    public partial class Form1 : Form
    {
        
       

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //This line simply shows in a datagridview me that data is being found
            
            this.table1TableAdapter.Fill(this.database1DataSet.Table1);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\bin\\Debug\\Database1.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Table1 (name, url) VALUES (@name, @url)", con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add("@name", SqlDbType.VarChar);
            cmd.Parameters["@name"].Value = textBox1.Text;

            cmd.Parameters.Add("@url", SqlDbType.VarChar);
            cmd.Parameters["@url"].Value = textBox2.Text;

            cmd.ExecuteNonQuery();

            con.Close();

            this.table1TableAdapter.Fill(this.database1DataSet.Table1);
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\bin\\Debug\\Database1.mdf;Integrated Security=True;User Instance=True");
            con.Open();

            string sql = ("SELECT * FROM TABLE1");
            DataSet ds1 = new DataSet();
            SqlDataAdapter dAdap = new SqlDataAdapter(sql, con);
            dAdap.Fill(ds1, "Table1");

            SqlCommandBuilder cBuild = new SqlCommandBuilder(dAdap);

            DataRow dRow = ds1.Tables["Table1"].NewRow();
            dRow[0] = textBox1.Text;
            dRow[1] = textBox2.Text;
            ds1.Tables["Table1"].Rows.Add(dRow);

            dAdap.Update(ds1, "Table1");

            con.Close();

            this.table1TableAdapter.Fill(this.database1DataSet.Table1);

        }
    }
}
Posted
Updated 15-Mar-10 9:15am
v2

1 solution

A couple of issues here.

1. AttachDbFilename=|DataDirectory|\\bin\\Debug\\Database1.mdf
This bit |DataDirectory| refers to the default data directory for sql server not your app.

2. Permissions, by default sql server uses NETWORK SERVICE as its user account this normally doesn't have access to the user folders, so you may need to give it access.

As a quick test just put the mdf file in the root of C: and use AttachDbFilename=C:\\Database1.mdf and see if that works.
 
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