Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please solve this problem
C#
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient; 
using System.Text;
using System.Windows.Forms;

namespace empinformation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt;
        int currenrec = 0;
        int totalrec = 0;
       static SqlConnection con = new SqlConnection("user id=sa; password=123;database=work");

       static SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        private void button1_Click(object sender, EventArgs e)
        {
            
            da.Fill(ds, "emp");
            dt = ds.Tables["emp"];
            currenrec = 0;
            totalrec = dt.Rows.Count;
            FillControls();
            btnext.Enabled = true;
            btprev.Enabled = true;
        }
        private void FillControls()
        {
            TextBox1.Text = dt.Rows[currenrec]["empno"].ToString();
            TextBox2.Text = dt.Rows[currenrec]["ename"].ToString();
            TextBox3.Text = dt.Rows[currenrec]["job"].ToString();
            TextBox4.Text = dt.Rows[currenrec]["sal"].ToString();
            TextBox5.Text = dt.Rows[currenrec]["hiredate"].ToString();
           TextBox6.Text = dt.Rows[currenrec]["comm"].ToString();
           TextBox7.Text = dt.Rows[currenrec]["deptno"].ToString();
        }

        private void btnext_Click(object sender, EventArgs e)
        {
            currenrec += 1;
            if (currenrec >= totalrec)
            {
                currenrec = 0;
            }
            FillControls();
        }

        private void btprev_Click(object sender, EventArgs e)
        {

            currenrec -= 1;
            if (currenrec <= totalrec)
            {
                currenrec = totalrec-1;
            }
            FillControls();
        }

        private void btnew_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno) ";
            cmd.Parameters.AddWithValue("@empno",TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename",TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept",TextBox7.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
Posted
Updated 8-Jun-12 18:10pm
v2

assign connection object to cmd

cmd.Connection = con;

see below code

C#
cmd.CommandText = "insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno) ";
            cmd.Parameters.AddWithValue("@empno", TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename", TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept", TextBox7.Text);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


Also use same parameter in query and in command parameter

cmd.Parameters.AddWithValue("@sal", TextBox4.Text);

Hope this will help you
 
Share this answer
 
v3
Comments
Sandeep Mewara 9-Jun-12 14:29pm    
5!
please give connection object while your insert query.


C#
private void btnew_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = ("insert into emp values (@empno,@ename,@job,@sal,@hiredate,@comm,@deptno)",con);
            cmd.Parameters.AddWithValue("@empno",TextBox1.Text);
            cmd.Parameters.AddWithValue("@ename",TextBox2.Text);
            cmd.Parameters.AddWithValue("@job", TextBox3.Text);
            cmd.Parameters.AddWithValue("@salary", TextBox4.Text);
            cmd.Parameters.AddWithValue("@hiredate", TextBox5.Text);
            cmd.Parameters.AddWithValue("@comm", TextBox6.Text);
            cmd.Parameters.AddWithValue("@dept",TextBox7.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
        }



hope this will hep you, if not please Post it.
 
Share this answer
 
v3
Comments
amperayani 9-Jun-12 0:29am    
it is saying ,invalid ;,invalid ) so again so many error coming
what to do
[no name] 9-Jun-12 0:41am    
please post whole error and try updated solution once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900