Search My Warehouse

2010-01-09

model

form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;

namespace Text_Categorization
{
public partial class Form1 : Form
{
Funtions f = new Funtions();
string ConnStr = ConfigurationSettings.AppSettings["connect2DB"].ToString();
string PassQuery,Result;
int ResultInt,SplitID;
SqlConnection con = new SqlConnection();

public Form1()
{
InitializeComponent();
}

private void btnSave_Click(object sender, EventArgs e)
{
if (f.ErrProvider_AllFields(errorProvider1, this, "Mandatory") == true)
{
PassQuery = "INSERT INTO USERDETAIL VALUES('" + lblUserID.Text + "','" + txtUn.Text.Replace("'", "''") + "','" + txtPwd.Text.Replace("'", "''") + "','" + txtNickname.Text.Replace("'", "''") + "','" + dateDob.Text + "','" + comGender.SelectedItem.ToString() + "','" + txtAddr.Text.Replace("'", "''") + "','" + txtMob.Text.Replace("'", "''") + "','" + txtMail.Text.Replace("'", "''") + "','"+ comCountry.SelectedItem.ToString() +"',0)";
ResultInt = f.ExecQry(con, ConnStr, PassQuery);
if (ResultInt == 1)
MessageBox.Show("User Registered.");
else
MessageBox.Show("Not User Registered.");
}
}

private void Form1_Load(object sender, EventArgs e)
{
comGender.SelectedIndex = 0;
PassQuery = "SELECT MAX(UID) FROM USERDETAIL";
Result = Convert.ToString(f.GetSingle(con, ConnStr, PassQuery));
lblUserID.Text = "TC" + Convert.ToString(Result.Substring(2, 4));
}
}
}


cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

namespace Text_Categorization
{
class Funtions
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
SqlDataReader dr;
DataSet ds;
DataTable dt;
int ResultStatus,Counter;
string ResultStr = "";
object ReturnObj = "";
string errorText;
bool bStatus;

public SqlConnection DBconnect(SqlConnection conn, string Constr)
{
if (conn.State != ConnectionState.Open)
{
conn = new SqlConnection(Constr);
conn.Open();
}
return conn;
}

public int ExecQry(SqlConnection conn,string ConnectStr, string Qry)
{
try
{
cmd = new SqlCommand(Qry, DBconnect(conn, ConnectStr));
return ResultStatus = cmd.ExecuteNonQuery();
}
catch
{
return ResultStatus = 0;
}
finally { conn.Close(); conn.Dispose(); }
}

public object GetSingle(SqlConnection conn, string ConnectStr, string Qry)
{
try
{

cmd = new SqlCommand(Qry, DBconnect(conn, ConnectStr));
return ReturnObj = cmd.ExecuteScalar();
}
catch
{
return ReturnObj;
}
finally { conn.Close(); conn.Dispose(); }
}

public DataTable GetDT(SqlConnection conn, string ConnectStr, string Qry)
{
try
{
da = new SqlDataAdapter(Qry, DBconnect(conn, ConnectStr));
da.Fill(dt);
return dt;
}
catch
{
dt.Clear();
return dt;
}
finally
{
conn.Close();
con.Dispose();
}
}

public bool ErrProvider_Combo(ComboBox ComBx, ErrorProvider errorProvider1, string msg)
{
bStatus = true;
if (ComBx.SelectedIndex == 0)
{
errorProvider1.SetError(ComBx, msg);
bStatus = false;
}
else
errorProvider1.SetError(ComBx, "");
return bStatus;
}

public bool ErrProvider_TextBox(TextBox TxtBX, ErrorProvider errorProvider1, string msg)
{
bool bStatus = true;
if (TxtBX.Text == "")
{
errorProvider1.SetError(TxtBX, msg);
bStatus = false;
}
else
errorProvider1.SetError(TxtBX, "");
return bStatus;
}

public bool ErrProvider_AllFields(ErrorProvider errorProvider1, Form ctr,string ErrMsg)
{
Counter=0;
bStatus=true;
foreach (Control ctrl in ctr.Controls)
{
if (ctrl.Text == "")
{
errorProvider1.SetError(ctrl, ErrMsg);
Counter++;
}
}
if (Counter != 0)
return bStatus = false;
else
return bStatus = true;
}

}
}



Feed