Send & Get Data to SQL Database Using c#


Saturday, March 8, 2014

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet objds = new DataSet();

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_details";
        cmd.Parameters.AddWithValue("@select", "select");

        conn.Open();
        SqlDataAdapter objda = new SqlDataAdapter(cmd);
        objda.Fill(objds);
        conn.Close();
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        Boolean checkresult = new Boolean();

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SP_details";

        cmd.Parameters.AddWithValue("@select", "insert");
        cmd.Parameters.AddWithValue("@fname", txtfname.Text);
        cmd.Parameters.AddWithValue("@lname", txtlname.Text);

        conn.Open();
        checkresult = Convert.ToBoolean(cmd.ExecuteNonQuery());
        conn.Close();
    }
}






Connection String ----------


<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=RAJESH-PC;Persist Security Info=True;Initial Catalog=test;User ID=sa;Password=xyz" providerName="System.Data.SqlClient"/>
</connectionStrings>


Order by on Varchar field in sql server


Saturday, March 17, 2012

Hello Reader,

In this articles, i am going to explain you, how to use Order by on Varchar Fields in SQL Server,

Till now you might have implemented order by on Int Fields but you may find difficult to implement order by on Varchar fields.


In Image you can see, i have create one field with Name ONO as varchar(50)..

Now if you will do directly order by like shown below, then you will get wrong data.

This way is wrong for Varchar Field..
select * from db_product DP
order by ONO

You will get wrong data as Output..



The correct way is there..
select * from db_product DP
order by 
case 
    IsNumeric(DP.ONO) when 1 then 
        Replicate(Char(0), 100 - Len(DP.ONO)) + DP.ONO 
    else 
        DP.ONO 
end


Thanks Hope this post will help you, if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Email: raj143svmit@gmail.com
Dobazaar (Dubai, UAE)
www.dobazaar.com


Validation of viewstate MAC failed.


Friday, December 2, 2011

Question : Getting this error, "Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."

Solution :
In your web.config file, you will find this line.



Just replace this line with below line and all your problem solved.



Please send me your questions to me, if you have any..

Thanks Hope this post will help you, if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Email: raj143svmit@gmail.com
Dobazaar (Dubai, UAE)
www.dobazaar.com

301 redirect "/Default.aspx" to "/" Root Asp.Net


Sunday, October 30, 2011

Question : How to use global.asax and do 301 redirect "/Default.aspx" to "/" Root ?
Answer :

Add below code in global.asax, thats it.


void Application_BeginRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://induway.com"))
        {
            HttpContext.Current.Response.Status = "301 Moved Permanently";
            HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://induway.com/default.aspx", "http://www.induway.com"));
        }
    }



Let me know, if you have any query..


Thanks Hope this post will help you, if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Email: raj143svmit@gmail.com
Dobazaar (Dubai, UAE)
www.dobazaar.com

Create, Read, and Detele Cookies in Javascript


Thursday, September 22, 2011

Hello Friends,

This post will explain you how you can create, read and delete the cookies in your webpage using Javascript.

Here is the code to create cookies,
In this below code, you can see, Cookies is Set to Name "RajeshCookie" and value is set to "Rajesh" and
its expire time is set to next month as shown in below code.


//for creating a cookie
<script type="text/javascript">
<!--
   var today = new Date();
   var nextMonth = new Date(today.getYear(), today.getMonth()+1, today.getDate());
   setCookie("RajeshCookie", "Rajesh", nextMonth);
// -->
</script>


Here is the code to read cookies,


//for reading the cookie
<script type="text/javascript">
<!--
      var szName = getCookie("RajeshCookie");
// -->
</script>


Here is the code to Delete cookies,


//for deleting the cookie
<script type="text/javascript">
<!--
      deleteCookie('RajeshCookie');
// -->
</script>


Let me know, if you have any query..




Thanks Hope this post will help you, if yes please put comment below of this page,
Rajesh Singh,
Senior Asp.Net Developer
e-Procurement Technologies Ltd (India)
www.abcprocure.com