User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 427,935 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,936 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 1051 | Replies: 5
Reply
Join Date: Jun 2008
Posts: 3
Reputation: mridatrix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mridatrix mridatrix is offline Offline
Newbie Poster

Unable to insert Value into database

  #1  
Jul 8th, 2008
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....
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 114
Reputation: mwasif is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 14
mwasif mwasif is offline Offline
Junior Poster

Re: Unable to insert Value into database

  #2  
Jul 8th, 2008
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')";
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: mridatrix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mridatrix mridatrix is offline Offline
Newbie Poster

Re: Unable to insert Value into database

  #3  
Aug 6th, 2008
Originally Posted by mwasif View Post
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
Reply With Quote  
Join Date: Jul 2008
Location: Sweet India
Posts: 849
Reputation: Shanti Chepuru is on a distinguished road 
Rep Power: 2
Solved Threads: 74
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Practically a Posting Shark

Re: Unable to insert Value into database

  #4  
Aug 8th, 2008
please once try this:
 string sql = " INSERT into `employee`(`fname`, `mname`,` lname`) VALUES (@fname, @mname,@lname)";
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: mridatrix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mridatrix mridatrix is offline Offline
Newbie Poster

Re: Unable to insert Value into database

  #5  
Aug 9th, 2008
Originally Posted by Shanti Chepuru View Post
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
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,467
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Unable to insert Value into database

  #6  
Aug 10th, 2008
@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
  1. try
  2. {
  3. conn.Open();
  4. cmd.ExecuteNonQuery();
  5. }
  6. catch (SqlException ex)
  7. {
  8. errorMessage();
  9. }
  10. finally
  11. {
  12. conn.Close();
  13. // Just simple notification
  14. DialogResult result =
  15. MessageBox.Show("New book added to database", "Database Message",
  16. MessageBoxButtons.OK, MessageBoxIcon.Information, 0, 0);
  17. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 6:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC