8 Replies - 530 Views - Last Post: 11 March 2010 - 06:27 AM Rate Topic: -----

#1 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

values are not adding in sql database

Posted 10 March 2010 - 09:51 PM

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;

namespace libarymangementsystem
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da1;
        DataSet ds;
        string cstr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cstr = "server=DESKTOP;user id=sa;password=admin;database=master";
            ds = new DataSet();
            Fill1();

        }

        private void Fill1()
        {

            string qry = "select * from Employe_details";
            SqlConnection con = new SqlConnection(cstr);
            da1 = new SqlDataAdapter(qry, con);
            da1.Fill(ds, "Employe_details");

        }


        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (validate1())
                {

                    DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = textBox5.Text;

                    tab.Rows.Add(row);
                    MessageBox.Show("Your record has been successfully added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
            da1.InsertCommand = cb1.GetInsertCommand();
            da1.UpdateCommand = cb1.GetUpdateCommand();
            da1.DeleteCommand = cb1.GetDeleteCommand();
            da1.Update(ds, "Employe_details");
        }


        private bool validate1()
        {

            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0 && textBox5.Text.Length > 0)
                return true;
            else
            {
                if (textBox1.Text == "")
                    MessageBox.Show("Roll Number field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox2.Text == "")
                    MessageBox.Show(" First Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox3.Text == "")
                    MessageBox.Show("Last Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox4.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox5.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (validate1())
            {
                DataTable tab = ds.Tables["personal_details"];
                DataRow[] rows;
                rows = tab.Select("Roll_No='" + textBox1.Text + "'");
                rows[0][1] = textBox2.Text;
                rows[0][2] = textBox3.Text;
                rows[0][3] = textBox4.Text;
                rows[0][4] = textBox5.Text;

                button1.Enabled = false;

                MessageBox.Show("Your record has been successfully updated");
            }

        }
    }
}



compiler is not giving any error can you provide hint where i am gong wrong man


value i send using form not added in sql table

Is This A Good Question/Topic? 0
  • +

Replies To: values are not adding in sql database

#2 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: values are not adding in sql database

Posted 10 March 2010 - 10:03 PM

Place some breakpoints in the Fill1 method, button_click events, and the formclosing event. Run it in Debug mode and see if the DataSet has changed after the button is clicked.
Was This Post Helpful? 1
  • +
  • -

#3 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: values are not adding in sql database

Posted 10 March 2010 - 10:11 PM

        private void Fill1()
        {

            string qry = "select * from Employe_details";
            SqlConnection con = new SqlConnection(cstr);
            da1 = new SqlDataAdapter(qry, con);
            da1.Fill(ds, "Employe_details");

        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
            da1.InsertCommand = cb1.GetInsertCommand();
            da1.UpdateCommand = cb1.GetUpdateCommand();
            da1.DeleteCommand = cb1.GetDeleteCommand();
            da1.Update(ds, "Employe_details");
        }



the main problem is here because i open in select mode so can not insert in it how to solve this
Was This Post Helpful? 0
  • +
  • -

#4 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: values are not adding in sql database

Posted 10 March 2010 - 10:21 PM

need help ?
Was This Post Helpful? 0
  • +
  • -

#5 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: values are not adding in sql database

Posted 10 March 2010 - 11:04 PM

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;

namespace libarymangementsystem
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da1;
        DataSet ds;
        string cstr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cstr = "server=DESKTOP;user id=sa;password=admin;database=master";

            ds = new DataSet();
            Fill1();




        }

        private void Fill1()
        {

            string qry = "select * from Employe_details";
            SqlConnection con = new SqlConnection(cstr);
            da1 = new SqlDataAdapter(qry, con);
            da1.Fill(ds, "Employe_details");

        }


        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (validate1())
                {

                    DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = textBox5.Text;

                    tab.Rows.Add(row);
                    MessageBox.Show("Your record has been successfully added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        private bool validate1()
        {

            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0 && textBox5.Text.Length > 0)
                return true;
            else
            {
                if (textBox1.Text == "")
                    MessageBox.Show("Roll Number field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox2.Text == "")
                    MessageBox.Show(" First Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox3.Text == "")
                    MessageBox.Show("Last Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox4.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox5.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return false;

        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
            da1.InsertCommand = cb1.GetInsertCommand();
            da1.UpdateCommand = cb1.GetUpdateCommand();
            da1.DeleteCommand = cb1.GetDeleteCommand();
            da1.Update(ds, "Employe_details");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("sam");
        }
    }
}




sql table
drop table Employe_details
create table Employe_details(Eid int  primary key,name varchar(30),address varchar(30),phoneno int ,dept varchar(30))


insert into Employe_details values(12,'dfs','fds',23,'fg')

select * from Employe_details




can any one tell me what is wrong with this code
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1866
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: values are not adding in sql database

Posted 10 March 2010 - 11:09 PM

Only if you tell us what the errors are. We're not mind readers...
Was This Post Helpful? 2
  • +
  • -

#7 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: values are not adding in sql database

Posted 10 March 2010 - 11:13 PM

the values are not getting add in sql table ??


where i am stucked
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: values are not adding in sql database

Posted 11 March 2010 - 01:43 AM

It is possible that your cb1.GetInsertCommand() method does not really get the insert statement... Check the dataAdapter properties after the call to cb1.GetInsertCommand() and see if insert statement has been set or not...
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: values are not adding in sql database

Posted 11 March 2010 - 06:27 AM

I have merged both topics. Please don't create multiple topics for the same problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1