i was just doing a simple thing in C#, just trying to put the values feeded in the text boxes in sql database. i am giving the code below the code executed perfectly but when i went to look into the table data there was no data inserted...... pls HELP...!!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace txtbox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn=new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Details.mdf;Integrated Security=True;User Instance=True");
            string sql = " INSERT into employee(fname, mname, lname) VALUES (@fname, @mname,@lname)";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.Add(new SqlParameter("@fname", SqlDbType.NVarChar,50));
            cmd.Parameters.Add(new SqlParameter("@mname", SqlDbType.NVarChar,50));
            cmd.Parameters.Add(new SqlParameter("@lname", SqlDbType.NVarChar,50));

            cmd.Parameters[0].Value = textBox1.Text;
            cmd.Parameters[1].Value = textBox2.Text;
            cmd.Parameters[2].Value = textBox3.Text;
            cn.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("inserted successfully");
        }
    }
}

at the end i get the message in the message box "inserted successfully" but there is no value inserted in the table....

String values must be enclosed with single quotes e.g.

INSERT into employee(fname, mname, lname) VALUES ('name', 'mname','name')

You may try similar to this

string sql = " INSERT into employee(fname, mname, lname) VALUES ('@fname', '@mname','@lname')";

String values must be enclosed with single quotes e.g.

INSERT into employee(fname, mname, lname) VALUES ('name', 'mname','name')

You may try similar to this

string sql = " INSERT into employee(fname, mname, lname) VALUES ('@fname', '@mname','@lname')";

Still not able to insert values into database table ....
whts the problem i cant figure it out ....
kindly help

please once try this:

string sql = " INSERT into `employee`(`fname`, `mname`,` lname`) VALUES (@fname, @mname,@lname)";

please once try this:

string sql = " INSERT into `employee`(`fname`, `mname`,` lname`) VALUES (@fname, @mname,@lname)";

i knew this wont work but gave it a try ..... it still does not solves the problem

@mridatrix - your coding is wrong. Database connectivity in any programming language is always associated with try & catch block often accompanied by final . Something similar to this example

try
{
     conn.Open();
     cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
     errorMessage();
}
finally
{
     conn.Close();
     // Just simple notification
     DialogResult result =
         MessageBox.Show("New book added to database", "Database Message",
          MessageBoxButtons.OK, MessageBoxIcon.Information, 0, 0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.