Thursday, 29 March 2012

Inserting and deleting and update records in MSSQL Server database using .net C#

Check duplicate before Inserting record:
SqlConnection scon = new SqlConnection("Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True");
        scon.Open();
        SqlCommand scmd = new SqlCommand("select PO from PO_Table where PO=@PO " , scon);
        scmd.Parameters.Add("PO", SqlDbType.Int).Value = TextBox1.Text;
        scmd.ExecuteScalar();

        if (scmd.ExecuteScalar()== null)
        {
            SqlDataAdapter da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand("insert into PO_Table values (@PO, @Style_Ref, @Price, @Season, @Order_Qty, @PO_Ship_Date, @PO_Shipping_Month, @PO_Shipping_Mode, @PO_Value, @PO_Destination, @Vendor, @Buyer)", scon);
            da.InsertCommand.Parameters.Add("@PO", SqlDbType.Int).Value = TextBox1.Text;
            da.InsertCommand.Parameters.Add("@Style_Ref", SqlDbType.NVarChar).Value = TextBox2.Text;
            da.InsertCommand.Parameters.Add("@PO_Destination", SqlDbType.NVarChar).Value = DropDownList3.Text;
            da.InsertCommand.Parameters.Add("@Price", SqlDbType.Float).Value = TextBox3.Text;
            da.InsertCommand.Parameters.Add("@Season", SqlDbType.NVarChar).Value = DropDownList2.Text;
            da.InsertCommand.Parameters.Add("@Order_Qty", SqlDbType.Int).Value = TextBox4.Text;
            da.InsertCommand.Parameters.Add("@PO_Ship_Date", SqlDbType.DateTime).Value = TextBox5.Text;
            da.InsertCommand.Parameters.Add("@PO_Shipping_Month", SqlDbType.Char).Value = TextBox6.Text;
            da.InsertCommand.Parameters.Add("@PO_Shipping_Mode", SqlDbType.Char).Value = TextBox7.Text;
            da.InsertCommand.Parameters.Add("@PO_Value", SqlDbType.Int).Value = TextBox8.Text;
            da.InsertCommand.Parameters.Add("@Vendor", SqlDbType.NVarChar).Value = DropDownList5.Text;
            da.InsertCommand.Parameters.Add("@Buyer", SqlDbType.NVarChar).Value = DropDownList4.Text;
            da.InsertCommand.ExecuteNonQuery();
            scon.Close();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            string strScript = "<script language='JavaScript'>alert('PO Enter Successfully')</script>";
            Page.RegisterStartupScript("PopUp", strScript);    
           
        }  
        else
        {
            string strScript = "<script language='JavaScript'>alert('PO Already Exist')</script>";
            Page.RegisterStartupScript("PopUp", strScript);         
           
           
        }      
            scon.Close();     


Clear fields after insterting records:
 TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";


Data Search and fill the fields:
SqlConnection scon = new SqlConnection("Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True");
        scon.Open();
        SqlCommand scmd = new SqlCommand("select PO from PO_Table where PO=@PO ", scon);
        scmd.Parameters.Add("PO", SqlDbType.Int).Value = TextBox1.Text;
        scmd.ExecuteScalar();
       
        if (scmd.ExecuteScalar() == null)
       
        {
            string strScript = "<script language='JavaScript'>alert('PO Not Found, Please Re-enter Your PO No')</script>";
            Page.RegisterStartupScript("PopUp", strScript);
            scon.Close();
       }
           
        else
        {
            SqlConnection scon1 = new SqlConnection("Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True");
            SqlCommand scmd1 = new SqlCommand("select * from PO_Table where PO=@PO", scon1);
            scmd1.Parameters.Add("PO", SqlDbType.Int).Value = TextBox1.Text;
            SqlDataAdapter sda = new SqlDataAdapter(scmd1);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();
            TextBox2.Text = ds.Tables[0].Rows[0][1].ToString();
            TextBox3.Text = ds.Tables[0].Rows[0][2].ToString();
            TextBox4.Text = ds.Tables[0].Rows[0][4].ToString();
            TextBox5.Text = ds.Tables[0].Rows[0][5].ToString();
            TextBox6.Text = ds.Tables[0].Rows[0][6].ToString();
            TextBox7.Text = ds.Tables[0].Rows[0][7].ToString();
            TextBox8.Text = ds.Tables[0].Rows[0][8].ToString();
            DropDownList2.Text = ds.Tables[0].Rows[0][3].ToString();
            DropDownList3.Text = ds.Tables[0].Rows[0][9].ToString();
            DropDownList5.Text = ds.Tables[0].Rows[0][10].ToString();
            DropDownList4.Text = ds.Tables[0].Rows[0][11].ToString();
            scon1.Close();
                      
Update Data with aleart:
SqlConnection use = new SqlConnection(" Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True ");
        SqlDataAdapter da = new SqlDataAdapter();
        da.UpdateCommand = new SqlCommand("update PO_Table set PO=@PO, Vendor=@Vendor, Buyer=@Buyer, Style_Ref=@Style_Ref, Price=@Price, Season=@Season, Order_Qty=@Order_Qty, PO_Ship_Date=@PO_Ship_Date, PO_Shipping_Month=@PO_Shipping_Month, PO_Shipping_Mode=@PO_Shipping_Mode, PO_Value=@PO_Value, PO_Destination=@PO_Destination where PO=@PO", use);
        da.UpdateCommand.Parameters.Add("@PO", SqlDbType.Int).Value = TextBox1.Text;
        da.UpdateCommand.Parameters.Add("@Style_Ref", SqlDbType.NVarChar).Value = TextBox2.Text;
        da.UpdateCommand.Parameters.Add("@PO_Destination", SqlDbType.NVarChar).Value = DropDownList3.Text;
        da.UpdateCommand.Parameters.Add("@Price", SqlDbType.Money).Value = TextBox3.Text;
        da.UpdateCommand.Parameters.Add("@Season", SqlDbType.NVarChar).Value = DropDownList2.Text;
        da.UpdateCommand.Parameters.Add("@Order_Qty", SqlDbType.Int).Value = TextBox4.Text;
        da.UpdateCommand.Parameters.Add("@PO_Ship_Date", SqlDbType.DateTime).Value = TextBox5.Text;
        da.UpdateCommand.Parameters.Add("@PO_Shipping_Month", SqlDbType.Char).Value = TextBox6.Text;
        da.UpdateCommand.Parameters.Add("@PO_Shipping_Mode", SqlDbType.Char).Value = TextBox7.Text;
        da.UpdateCommand.Parameters.Add("@PO_Value", SqlDbType.Int).Value = TextBox8.Text;
        da.UpdateCommand.Parameters.Add("@Vendor", SqlDbType.NVarChar).Value = DropDownList5.Text;
        da.UpdateCommand.Parameters.Add("@Buyer", SqlDbType.NVarChar).Value = DropDownList4.Text;
        use.Open();
        da.UpdateCommand.ExecuteNonQuery();
        use.Close();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        string strScript = "<script language='JavaScript'>alert('Record Update')</script>";
        Page.RegisterStartupScript("PopUp", strScript);
Delete Record:

 SqlConnection use = new SqlConnection(" Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True ");
        SqlDataAdapter da = new SqlDataAdapter();
        da.DeleteCommand = new SqlCommand("delete from PO_Table where PO=@PO", use);
        da.DeleteCommand.Parameters.Add("@PO", SqlDbType.Int).Value = TextBox1.Text;
        da.DeleteCommand.Parameters.Add("@Style_Ref", SqlDbType.NVarChar).Value = TextBox2.Text;
        da.DeleteCommand.Parameters.Add("@PO_Destination", SqlDbType.NVarChar).Value = DropDownList3.Text;
        da.DeleteCommand.Parameters.Add("@Price", SqlDbType.Money).Value = TextBox3.Text;
        da.DeleteCommand.Parameters.Add("@Season", SqlDbType.NVarChar).Value = DropDownList2.Text;
        da.DeleteCommand.Parameters.Add("@Order_Qty", SqlDbType.Int).Value = TextBox4.Text;
        da.DeleteCommand.Parameters.Add("@PO_Ship_Date", SqlDbType.DateTime).Value = TextBox5.Text;
        da.DeleteCommand.Parameters.Add("@PO_Shipping_Month", SqlDbType.Char).Value = TextBox6.Text;
        da.DeleteCommand.Parameters.Add("@PO_Shipping_Mode", SqlDbType.Char).Value = TextBox7.Text;
        da.DeleteCommand.Parameters.Add("@PO_Value", SqlDbType.Int).Value = TextBox8.Text;
        da.DeleteCommand.Parameters.Add("@Vendor", SqlDbType.NVarChar).Value = DropDownList5.Text;
        da.DeleteCommand.Parameters.Add("@Buyer", SqlDbType.NVarChar).Value = DropDownList4.Text;
        use.Open();
        da.DeleteCommand.ExecuteNonQuery();
        use.Close();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
Required Feild Validation:
<asp:RequiredFieldValidator runat="server" id="RFV" ControlToValidate="TextBox1" ErrorMessage="PO No Is Required" display="Static">
        </asp:RequiredFieldValidator>
Record Delete Alert

<asp:Button ID="Button5" Text="Delete" runat="server" Width="100px" onclick="Button5_Click" onclientclick="return confirm ('Are You Sure You Want To Delete This PO')" /> 

Wednesday, 28 March 2012

insert and updating delete and select using oledb access database in .net

using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    OleDbDataAdapter da;
    DataSet ds;
   
    protected void Insert_Click(object sender, EventArgs e)
    {
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Learning2\\App_Data\\tahir.accdb;Persist Security Info=False;");
        OleDbCommand comm = new OleDbCommand("INSERT INTO test VALUES(@roll_no, @st_name, @st_course)", conn);
        comm.Parameters.AddWithValue("@roll_no", TextBox1.Text);
        comm.Parameters.AddWithValue("@st_name", TextBox2.Text);
        comm.Parameters.AddWithValue("@st_course", TextBox3.Text);
        //ds = new DataSet();
        //da = new OleDbDataAdapter(comm);
        //da.Fill(ds);
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Learning2\\App_Data\\tahir.accdb;Persist Security Info=False;");
    OleDbCommand comm = new OleDbCommand("delete * from test where roll_no=@rollno ", conn);
    comm.Parameters.AddWithValue("@rollno", TextBox1.Text);
    conn.Open();
    comm.ExecuteNonQuery();
      
    }
    protected void Update_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Learning2\\App_Data\\tahir.accdb;Persist Security Info=False;");
        OleDbCommand comm = new OleDbCommand("update test set st_name='Tahir' where roll_no=@rollno ", conn);
        comm.Parameters.AddWithValue("@rollno", TextBox1.Text);
        conn.Open();
        comm.ExecuteNonQuery();
      }
    protected void Display_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Learning2\\App_Data\\tahir.accdb;Persist Security Info=False;");
        OleDbCommand comm = new OleDbCommand("select * from test where roll_no=@rollno ", conn);
        comm.Parameters.AddWithValue("@rollno",TextBox1.Text);
        da = new OleDbDataAdapter(comm);
        ds = new DataSet();
        da.Fill(ds);
       // GridView1.DataSource = ds;
       // GridView1.DataBind();
       // GridView1.Visible = true;
         TextBox2.Text = ds.Tables[0].Rows[0][1].ToString();
         TextBox3.Text = ds.Tables[0].Rows[0][2].ToString();
         da.Fill(ds);
        
       
    }
}

Wednesday, 21 March 2012

Inserting and connecting the MSAccess2007 with C# .net

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Learning2\\App_Data\\tahir.accdb;Persist Security Info=False;");
OleDbCommand comm = new OleDbCommand("INSERT INTO test VALUES(@roll_no, @st_name, @st_course)", conn);
comm.Parameters.AddWithValue("@roll_no", TextBox1.Text);
comm.Parameters.AddWithValue("@st_name", TextBox2.Text);
comm.Parameters.AddWithValue("@st_course", TextBox3.Text);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();

Wednesday, 14 March 2012

The best Example of Binary Operator Overloading in C++


#include<iostream>
using namespace std;

class Rational
{
private:
int num; // numerator
int den; // denominator
public:
void show();
Rational(int=1,int=1);
void setnumden(int,int);
Rational add(Rational object);
Rational operator+(Rational object);
bool operator==(Rational object);
bool operator!=(Rational object);
};

void Rational::show() {
cout << num << "/" << den << "\n";
}

Rational::Rational(int a,int b) {
setnumden(a,b);
}

void Rational::setnumden(int x,int y) {
int temp,a,b;
a = x;
b = y;
if(b > a) {
temp = b;
b = a;
a = temp;
}
while(a != 0 && b != 0) {
if(a % b == 0)
break;
temp = a % b;
a = b;
b = temp;
}
  num = x / b;
den = y / b;
}

Rational Rational::add(Rational object) {
int new_num = num * object.den + den * object.num;
int new_den = den * object.den;
return Rational(new_num, new_den);
}

Rational Rational::operator+(Rational object) {
int new_num = num * object.den + den * object.num;
int new_den = den * object.den;
return Rational(new_num, new_den);
}

bool Rational::operator==(Rational object) {
return (num == object.num && den == object.den);
}

bool Rational::operator!=(Rational object) {
return (num != object.num || den != object.den);
}

int main() {
Rational obj1(1,4), obj2(210,840), result1;
result1 = obj1.add(obj2);
result1.show();

Rational obj3(1,3), obj4(33,99), result2;
result2 = obj3 + obj4;
result2.show();

Rational obj5(10,14), obj6(25,35), obj7(2,3), obj8(1,2);

if(obj5 == obj6) {
cout << "The two fractions are equal." << endl;
}
if(obj7 != obj8) {
cout << "The two fractions are not equal." << endl;
}

return 0;
}