Wednesday, 21 May 2014

C# code for how to maintain the product Price history from the site admin

Indroduction

Now I will explain how to maintain the product Price history from the site admin.Let me explain the scenario like
“We have a all the product with essential details in the website. But  today the product price is some amount like 10 k after two month the site admin want to increase or change  the product price and also the site admin  want to maintain a entire price update history in database  not only last price history..That situation will go for below code”
Use the below C# code and Stored Procedure

Stored Procedure

Create procedure [dbo].[sp_pricehistory]

@upc bigint,
@pricedate date,
@changedprice money

AS
BEGIN
--Price update history start
if exists (select PM_Invbuylist from  Product_Master where PM_Invbuylist=@changedprice and PM_UPC =@upc)
begin
print 'not insert'
return -1
end

else
begin
print 'insert'

DECLARE @oldpricey money

SELECT @oldpricey = (select PM_Invbuylist from dbo.Product_Master where PM_UPC=@upc)

print @oldpricey

 insert into Price_History
(
PM_UPC,
PM_Pricechangedate,
PM_Changedprice

)
values
(
@upc,
@pricedate,
@oldpricey
                   
)
end
                                         

END


C# code to execute the stored procedure and changed price will stored or maintain in the other table
        
/// <summary>
        /// price history maintenence
        /// </summary>
        /// <param name="upc"></param>
        /// <param name="date"></param>
        /// <param name="price"></param>

        public void price_history(long upc, string date, string price)
        {
            if (CONNECTION.State == ConnectionState.Closed)
            {
                CONNECTION.Open();
            }
            SqlCommand cmd = new SqlCommand("sp_pricehistory", CONNECTION);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@upc", upc);
            cmd.Parameters.AddWithValue("@pricedate", DateTime.Today.ToString());
            cmd.Parameters.AddWithValue("@changedprice", price);
            cmd.ExecuteNonQuery();
        }


 I hope this will helpful for you! thanks

Tuesday, 20 May 2014

C# code for how to assign the function or method returns value to the variable




Indroduction

Now I will explain how to assign the function return value to the variable.
Use the below code
Use the below method and return the value
public decimal totalinvbuylists(decimal val1, decimal val2)
        {

            decimal response = val1 * val2;
            return decimal.Round(response, 2, MidpointRounding.AwayFromZero); ;
        }



//Pass the parameter to the method and receve the function returned value assign to the decimal value

//calculation part for totalinvbuylist

decimal Noofunit = Convert.ToDecimal(PM_Noofunit.Text);
decimal Invbuylist = Convert.ToDecimal(PM_Invbuylist.Text);
decimal calctotalinvbuylist = totalinvbuylists(Noofunit, Invbuylist);



I hope this will helpful for you! Thanks.

C# code for binding the database table in grid view (OR) C# code for edit the table data value under dataset (OR) how to edit the loaded dataset value dynamically



Indroduction
Now I will explain binding the database table in grid view (AND)
How to edit the table data value under dataset (AND) how to edit the loaded dataset value dynamically


Use the below code   and stored procedure

Stored Procedure
create procedure [dbo].[sp_bindproduct]
as begin

select *
from dbo.Product_Master

end

c# code
     
public void showgrid()
        {
//make connection to DB
            using (SqlConnection CON1 = new SqlConnection(@"Data Source=PROV-SER-001\SQLEXPRESS;Initial Catalog=Jeds;Integrated Security=True"))
            {


//call the stored procedure
                SqlCommand cmd_bindproduct = new SqlCommand("sp_bindproduct", CON1);

//execute the stored procedure

                cmd_bindproduct.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DA = new SqlDataAdapter(cmd_bindproduct);

//load the Db value into Dataset

                DataSet DS = new DataSet();
                DA.Fill(DS);
                //check the dataset if the value is 0 then become its N/A
//check dynamically              
 for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
                {
//load the coloumn value to string variable
                    string Svdiscountcost = DS.Tables[0].Rows[i]["PM_Svdiscountcost"].ToString();

                    if (Svdiscountcost == "0" || Svdiscountcost == null)
                    {
//Assign or edit the dataset value
                        DS.Tables[0].Rows[i]["PM_Svdiscountcost"] = "N/A";
                    }

                    string DiscountGM = DS.Tables[0].Rows[i]["PM_DiscountGM"].ToString();
                    if (DiscountGM == "0" || DiscountGM == null)
                    {
//Assign or edit the dataset value

                        DS.Tables[0].Rows[i]["PM_DiscountGM"] = "N/A";
                    }

                    string discountcostpallet = DS.Tables[0].Rows[i]["PM_Totaldiscountcost"].ToString();
                    if (discountcostpallet == "0" || discountcostpallet == null)
                    {
//Assign or edit the dataset value

                        DS.Tables[0].Rows[i]["PM_Totaldiscountcost"] = "N/A";
                    }


                }
//after edit the dataset Binding to the grid view
                GridView1.DataSource = DS;
                GridView1.DataBind();
            }
        }


I hope this will helpfull for you! Thanks

C# code for Reading the XML file content

C# code for Reading the XML file content


Indroduction
Now I will explain how to Read the XML file content in c#
.use the below code

 using System.Xml;

 XmlDocument doc = new XmlDocument();
 doc.Load("C:/Config/Connection.xml")
 XmlNodeList nodeList1 = doc.ChildNodes;
 string connection = nodeList1.Item(1).InnerText.ToString();

I hope this will helpful for you!Thanks.





Friday, 14 March 2014

Consuming the Webservice using JavaScript


Consuming the Webservice from the JavaScript

Indroduction
Now I will explain how to consume the webservices through javascript.
Let I explain the local host webservice it will work on remote webservices also(consuming third-party services)

Step1:
 create the Webservice
Solution explorer->add new item->asp.net web service
Step2:
 Write a below  code for web service

Step3:
Compile and run it without errors.Here I embed the below screen shot for understanding the soap request and response with parameter field .



Step 4:
What am going to implement is just am sending the “name “parameter from the client side to web service and get the response from the service.
Design and Javascript Code Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example.aspx.cs" Inherits="Example" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type ="text/javascript" >
 
    function callservice()
    {

        var xmlHTTP;
        if (window.XMLHttpRequest)
        {
            xmlHTTP = new window.XMLHttpRequest;

        }

        else {
                try {
                    xmlHTTP = new ActiveXObject("MSXML2.XMLHTTP.3.0");
                    }
                catch (ex)
                    {

                    }
             }

        var param = "Anandakumar";
        xmlHTTP.open("POST", "http://localhost:4921/Tickets1/WebService.asmx ", true);
        xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/Example");

        strRequest = "<?xml version='1.0' encoding='utf-8'?>";
        strRequest = strRequest + "<soap:Envelope " + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
        strRequest = strRequest + " <soap:Body>";
        strRequest = strRequest + "<Example xmlns='http://tempuri.org/'><name>'"+param+"'</name></Example>";
        strRequest = strRequest + "</soap:Body>";
        strRequest = strRequest + "</soap:Envelope>";
        alert(strRequest);
        xmlHTTP.onreadystatechange = function ()
        {
            if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200)
            {
                 alert(xmlHTTP.responseXML.xml);
            }
        }
        xmlHTTP.send(strRequest);
       
     }

</script>
<title>Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <p>
          <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick ="callservice()" />
         </p>
    </form>
   
</body>
</html>


Step 5:

Run under the IE you will be get a successful result like that



























Wednesday, 12 February 2014

Here I am gonna explain about how to checking the user authentication against user sql server database.

Step1:

Creating the very simple stored procedure for checking the user is authenticated one or not using below sql query.

create  procedure [dbo].[SP_Authentication]

@username  varchar(250),
@password  varchar(250)


as
begin

declare @count int
if  exists (select * from  userregister where u_name=@username  and u_password=@password )
begin
set @count = 1

end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 2:

Write a below c# code for checking the user is authenticated one or not.

  using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SP_Authentication", con);
            cmd.CommandType = CommandType.StoredProcedure;
            string password=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
            cmd.Parameters.Add("@username",TextBox1.Text);
            cmd.Parameters.Add("@password", password);

            int flag = (int)cmd.ExecuteScalar();

            if (flag == 1)
            {
                FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
            }

            else
            {   //not authenticated user
                Label1.Text = "Invalid User";
            }
            con.Close();

        }

 Output


                              
                                                            Thank you guys........!


Simple User Registration


Here I am gonna explain about how to registered user details into sql server database.

User Registration Steps:

Step 1:
Create the Table structure based on the below screen shot (table name: userregister)

   CREATE TABLE [dbo].[userregister]
   (
     [u_id] [int] IDENTITY(1,1) NOT NULL,
     [u_name] [varchar](250) NULL,
     [u_password] [varchar](250) NULL,
     [u_email] [varchar](250) NULL
     )

Step 2:

Create the very simple stored procedure for storing the user details and check whether the username is already exists or not in the database using SP_UserRegister stored Procedure

Create procedure [dbo].[SP_UserRegister]

@username  varchar(250),
@password  varchar(250),
@email     varchar(250)

as
begin

declare @count int
if not exists (select * from  userregister where u_name=@username)
begin
set @count = 1
insert into userregister values(@username,@password,@email)
end
else
begin
set @count=-1
end
select @count as returnvalue
end


Step 3:

Write a below C# code for supply the user details (username,password,etc..) parameter to stored procedure the procedure will take care of storing the user details to the Database table.

using (SqlConnection con = new SqlConnection(connectionstring ))
        {
            con.Open();
            SqlCommand cmd=new SqlCommand ("SP_UserRegister",con);
            cmd.CommandType = CommandType.StoredProcedure;

string pass= FormsAuthentication.HashPasswordForStoringInConfigFile(Textbox2.Text, "SHA1");

            cmd.Parameters.Add("@username", Textbox1.Text);
            cmd.Parameters.Add("@password", pass); ;
            cmd.Parameters.Add("@email", Textbox3.Text);

            int returncode = (int)cmd.ExecuteScalar();
                     
            con.Close();

            if (returncode == 1)
            {
                //user already not exist exist

                Label1.Text = "Register Sucessfully!";
            }
            else
            {
                // user already exist
                Label1.Text = " USER Already Exist ";
            }


 Output:



Thank you Guys....