using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class DataConnection
{
SqlConnection _connection = null;
SqlCommand _cmd = null;
public DataConnection()
{
_connection = new SqlConnection("Data Source=192.168.1.102\\serverpvt;Initial Catalog=aftab;User Id=sa;Password=abc123#");
_connection.Open();
}
public void CloseConnection()
{
if (_connection != null)
{
_connection.Close();
_connection = null;
}
}
public int ExecuteQuery(string psql)
{
int affectedrows = 0;
_cmd = new SqlCommand();
_cmd.Connection = _connection;
_cmd.CommandType = CommandType.Text;
_cmd.CommandText = psql;
affectedrows = _cmd.ExecuteNonQuery();
return affectedrows;
}
public SqlDataReader FetchQuery(string psql)
{
_cmd = new SqlCommand();
_cmd.Connection = _connection;
_cmd.CommandType = CommandType.Text;
_cmd.CommandText = psql;
SqlDataReader sreader = _cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sreader;
}
public SqlCommand CreateCommand(string sqlText, bool procedure)
{
_cmd = _connection.CreateCommand();
_cmd.CommandText = sqlText;
if (procedure)
_cmd.CommandType = CommandType.StoredProcedure;
return _cmd;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public static class CountryHandler
{
public static DataTable GetAll_Country()
{
DataTable dt = new DataTable();
DataConnection dcon = new DataConnection();
SqlCommand cmd = dcon.CreateCommand("GetAll_CountryDetails", true);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
public static DataTable Insert_Country(string pCountryName)
{
DataTable dtInsert = new DataTable();
DataConnection dcon = new DataConnection();
SqlCommand cmd = dcon.CreateCommand("Insert_Country", true);
cmd.Parameters.AddWithValue("@countryName", pCountryName);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtInsert);
return dtInsert;
}
public static DataTable Update_Country(string pCountryName, Int64 pCountryID)
{
DataTable dtInsert = new DataTable();
DataConnection dcon = new DataConnection();
SqlCommand cmd = dcon.CreateCommand("Update_Country", true);
cmd.Parameters.AddWithValue("@countryName", pCountryName);
cmd.Parameters.AddWithValue("@countryID", pCountryID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtInsert);
return dtInsert;
}
public static DataTable GetCountryDetailsByCountryName(string pCountryName)
{
DataTable dtCountryDetail = new DataTable();
DataConnection dcon = new DataConnection();
SqlCommand cmd = dcon.CreateCommand("Get_CountryDetail_By_CountryName", true);
cmd.Parameters.AddWithValue("@countryName", pCountryName);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtCountryDetail);
return dtCountryDetail;
}
}
protected void btnAddCountry_Click(object sender, EventArgs e)
{
try
{
if (txtCountry.Text.ToString() != "")
{
DataTable dtCountryDetails = new DataTable();
dtCountryDetails = CountryHandler.GetCountryDetailsByCountryName(txtCountry.Text.ToString());
if (dtCountryDetails.Rows.Count !=0)
{
if (dtCountryDetails.Rows.Count == 1)
{
DataTable dtUpdate = CountryHandler.Update_Country(txtCountry.Text.ToString(),Convert.ToInt64(dtCountryDetails.Rows[0]["country_Id"]));
ShowMessageBox("Update Data.");
txtCountry.Text = "";
txtCountry.Focus();
LoadCounty();
}
}
else
{
DataTable dtInsert = CountryHandler.Insert_Country(txtCountry.Text.ToString());
ShowMessageBox("Save Data.");
txtCountry.Text = "";
txtCountry.Focus();
LoadCounty();
}
}
else
{
txtCountry.Text = "";
txtCountry.Focus();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}