I have already connect my SQL database in C# windows application, Now, can somebody please give me sample codes for data manipulation, add/edit/delete of records....THANK you!

Please help me...here's so far I have...I need codes for add/edit/delete records....
thanx...

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 WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString ="server=sroque\\sqlexpress;database=InventoryDB;uid=sa;pwd=ripple";

            SqlConnection mySqlConnection = new SqlConnection(connectionString);

            string selectString = "SELECT * FROM tblUsers";

            SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
            mySqlCommand.CommandText = selectString;
            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

            mySqlDataAdapter.SelectCommand = mySqlCommand;

            DataSet myDataSet = new DataSet();

            mySqlConnection.Open();

            string dataTableName = "tblUsers";
            mySqlDataAdapter.Fill(myDataSet, dataTableName);

            DataTable myDataTable = myDataSet.Tables[dataTableName];

            foreach (DataRow myDataRow in myDataTable.Rows)
            {
                txtId.Text = "" + myDataRow["userID"];
                txtUser.Text = "" + myDataRow["Name"];
                txtPosition.Text = "" + myDataRow["Position"];
            }
            mySqlConnection.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "&Add")
            {
                txtId.Text = "";
                txtUser.Text = "";
                txtPosition.Text = "";
                btnAdd.Text = "&Save";
            }
            else if (btnAdd.Text == "&Save")
            {
                //then here I want to save records from the textbox to my database

            }
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

Please correct me if there's something wrong with my codes...It already have SQL Database connection and I successfully display my record from the database....

Please help me...here's so far I have...I need codes for data manipulation adding/edit/delete records....im using C#Windows Application

THANK YOU...you're a big 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;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string connectionString ="server=sroque\\sqlexpress;database=InventoryDB;uid=sa;pwd=ripple";

SqlConnection mySqlConnection = new SqlConnection(connectionString);

string selectString = "SELECT * FROM tblUsers";

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = selectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySqlCommand;

DataSet myDataSet = new DataSet();

mySqlConnection.Open();

string dataTableName = "tblUsers";
mySqlDataAdapter.Fill(myDataSet, dataTableName);

DataTable myDataTable = myDataSet.Tables[dataTableName];

foreach (DataRow myDataRow in myDataTable.Rows)
{
txtId.Text = "" + myDataRow["userID"];
txtUser.Text = "" + myDataRow["Name"];
txtPosition.Text = "" + myDataRow["Position"];
}
mySqlConnection.Close();
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (btnAdd.Text == "&Add")
{
txtId.Text = "";
txtUser.Text = "";
txtPosition.Text = "";
btnAdd.Text = "&Save";
}
else if (btnAdd.Text == "&Save")
{
//then here I want to save records from the textbox to my database

}
}
}
}

This question has already been answered. Start a new discussion instead.