Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hi i am learning new to the dotnet please anyone help me how to insert the data . i have written some code

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
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();
       
        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()
        {
            txtemp.Text = dt.Rows[currenrec]["empno"].ToString();
            txtename.Text = dt.Rows[currenrec]["ename"].ToString();
            txtjob.Text = dt.Rows[currenrec]["job"].ToString();
            txtsal.Text = dt.Rows[currenrec]["sal"].ToString();
            txthrd.Text = dt.Rows[currenrec]["hiredate"].ToString();
           txtcom.Text = dt.Rows[currenrec]["comm"].ToString();
           txtdept.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)
        {

            SqlCommand cmd = new SqlCommand();
        }

        private void btsve_Click(object sender, EventArgs e)
        {
          
        }
        private void btdel_Click(object sender, EventArgs e)
        {
            
        }
    }
}
Posted
Updated 13-May-12 4:36am
v2

Not sure what you wanted to ask from the code posted.

Look at these to learn on how to use ADO.NET to get data:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]
MSDN: DataAdapter Parameters (ADO.NET)[^]
 
Share this answer
 
Comments
Maciej Los 13-May-12 10:38am    
Good answer, my 5!
Sandeep Mewara 13-May-12 11:35am    
Thanks.
amperayani 13-May-12 10:47am    
hi i written the code for to display the data but i don't know how to insert the data in the button . i have writtend sqlconnection in general declaration. please help me
Maciej Los 13-May-12 10:57am    
Please, study examples (above links) and please, see my answer.
Monjurul Habib 13-May-12 15:32pm    
good links, my 5!
 
Share this answer
 
Comments
amperayani 13-May-12 11:02am    
i try that i got this error executeNonquery:connection prperly has not been initialized
private void btnew_Click(object sender, EventArgs e)
{

SqlCommand cmd = new SqlCommand("insert into emp values(empno,'ename','job',sal,'hiredate',comm,dept)");
con.Open();
cmd.ExecuteNonQuery();

}
Maciej Los 13-May-12 11:08am    
2 reasons:
1) SQL INSERT command is wrong!
insert into emp (HERE ENUM FIELDS) values(HERE ENUM VAULES)
2) an example how to create connection and insert data:
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
Maciej Los 13-May-12 11:09am    
If my solution was helpful, please, rate it.
amperayani 13-May-12 11:09am    
if i want to insert different data how it possible
Maciej Los 13-May-12 11:36am    
In the same way!
Please, read carefully linked articles...
Here you can get help on basic problem (if you have a problem and you can't find a solution).

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