locked
Insert into database problem RRS feed

  • Question

  • Hello I ve got a problem with inserting data into database.

    I have no error mesagges. I press button2 (to insert data) and then exit application , I run it  again, press button1(for view) but nothing happens (the textbox just stays empty.

    This is my code:

    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.SqlClient;
    using System.Data.Sql;
    using System.Data.Odbc;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {

            SqlConnection connection;
            string conectionstring;

            public Form1()
            {
                InitializeComponent();
                conectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Music.mdf;Integrated Security=True; User Instance=True";
                connection = new SqlConnection(conectionstring);
                try
                {
                    connection.Open();
                    Console.WriteLine("Connection open");
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee);
                }

            }

            private void button1_Click(object sender, EventArgs e)
            {
               

                SqlCommand command = new SqlCommand("SELECT * FROM Music WHERE Album='KISS'",connection);
                SqlDataReader reader = null;

                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    textBox1.Text = (reader["song"].ToString());

                }
              

               
            }

            private void button2_Click(object sender, EventArgs e)
            {
              
                 SqlCommand command = new SqlCommand("INSERT into Music (Album,Song) " + " Values ('KISS', 'Black Diamond')", connection);
                command.Connection = connection;
                command.ExecuteNonQuery();
               
             
            }

            private void button5_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

          
        }
    }
    Thursday, October 1, 2009 10:15 PM

All replies

  • Problem Solved !!!

    replace conectionstring

    conectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Music.mdf;Integrated Security=True; User Instance=True";

    with

    conectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Werther\Documents\Visual Studio 2008\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Music.mdf; Integrated Security=True; User Instance=True";
               
    Friday, October 2, 2009 10:22 AM
  • obviously as ver..  has specified the path for the database is totally missing...pls make the changes and run it should work. ur program is not having major changes...its only minor ones.

     

    sree

     

    MARK AS ANSWER IF UR DONE< ELSE REVERT BACK FOR MORE HELP

    Thursday, April 22, 2010 6:13 PM