6
Reply

[Help] I need to know how to use insert ,update ,delete

Miro Tech5

Miro Tech5

Mar 3 2016 11:58 AM
498
hello , I am Using Visual Studio C# 2010 and I am connecting it with SQL Express 2008 , 
I added class into my form . 
My Question Is : [ How to Use insert ,Update and Delete Statement also Try catch in my DataBase ?]
 
the class code: 
Form Code :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace Cal
{
class cCity
{
SqlConnection m_con;
private string TableName = "cities";
public cCity(SqlConnection Con)
{
m_con = Con;
}
public DataTable Fill()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = m_con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_" + TableName + "fill";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
public DataTable Select(int Id)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = m_con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Sp_" + TableName + "Select";
cmd.Parameters.Add("@cityid", SqlDbType.Int).Value = Id;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
public DataTable FillStates()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = m_con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Sp_StatesNameFill";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
}
}
and the FormCode  
 
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 Cal
{
public partial class FrmCities : Form
{
public FrmCities()
{
InitializeComponent();
}
cCity m_cCity;
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=SQL;Integrated Security=True");
private void FrmCities_Load(object sender, EventArgs e)
{
con.Open();
m_cCity = new cCity(con);
FillData();
FillStates();
}
private void FillData()
{
DataTable dt = m_cCity.Fill();
CboCityName.DisplayMember = "CityName";
CboCityName.ValueMember = "CityID";
CboCityName.DataSource = dt;
}
private void CboCityName_SelectedIndexChanged(object sender, EventArgs e)
{
if (CboCityName.SelectedIndex > -1)
{
DataTable dt = m_cCity.Select(Convert.ToInt32(CboCityName.SelectedValue));
txtCityNumber.Text = dt.Rows[0]["CityID"].ToString();
CboStatesName.SelectedValue = Convert.ToInt32(dt.Rows[0]["StateID"].ToString());
txtCityName.Text = dt.Rows[0]["Cityname"].ToString();
}
}
private void FillStates()
{
DataTable dt = m_cCity.FillStates();
CboStatesName.DisplayMember = "StatesName";
CboStatesName.ValueMember = "StateID";
CboStatesName.DataSource = dt;
}
private void btninsert_Click(object sender, EventArgs e)
{
}
private void btnnew_Click(object sender, EventArgs e)
{
}
private void btnsave_Click(object sender, EventArgs e)
{
}
private void btndelete_Click(object sender, EventArgs e)
{
}
}
}
 

Answers (6)