0

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....

4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by peter_budo
0

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')";
0

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

0

please once try this:

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

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

0

@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);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.