Thursday, 19 September 2013

Count function of SQL to count entries of various types saperately

Suppose you have a table 'A' in which you have '5' or many more categories and for the same type category you have many entries. Take an example where you want to count no of employes in corresponding departments and want to display them in web page.
How you will do it in Asp.Net?
I did that with the help of Grid View. I took a Grid View take two columns and used following SQL Query:
Select Department, Count(Name) as Emp_Name from table name group by Department having(Count(Name)>1)
By this way you can easily get No of employes in corresponding departments.

Wednesday, 18 September 2013

Insert Data In SQL Data Base using Jquery in Asp.Net

To make you understand take a small example. Suppose we want to make a simple signup form (in which we will not check similar username or duplicate user name; I hope you can easily do it). So make a table with two columns 'UserID' and 'Password'.
Take three text box one for username and other two to match and insert password (You can use java script to match password; I am not including it here). A button which will insert data in data base after clicking it.
Write this code after closing form tag and just above closing of body tag.
<script type="text/javascript">
        $(document).ready(function () {
            $("#Button1").click(function () {
                $.ajax({
                    type: 'post',
                    contentType: "application/json; charset=utf-8",
                    url: 'datainsertunsingjquery.aspx/InsertMethod',

              // Here InsertMethod is a function on page name datainsertusingjquery.aspx                  
             data: "{'UserID':'" + document.getElementById('TextBox1').value + "', 'Password':'" + document.getElementById('TextBox2').value + "'}",
                    async: false,
                    success: function (response) {
                        $('#TextBox1').val('');
                        $('#TextBox2').val('');
                        alert("Record Has been Saved in Database");
                    },
                    error: function ()

                    { console.log('there is some error'); }

                });
                    });

                         });
   
   
    </script>

And in the page of 'datainserusingjquery.aspx' write this function 'InsertMethod()'
[WebMethod]
    public static string InsertMethod(string UserID, string Password)
    {
        SqlConnection con = new SqlConnection("Pass here your Data base connection string");
       
       
        SqlCommand cmd = new SqlCommand("Insert into Login values('" + UserID + "', '" + Password + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        return "True";
    }

Tuesday, 17 September 2013

How to Hide Buttons On Hardcopy of Printed Page and also Hide URL and Date

To hide URL and date in hard copy of printed page you should do some setting in your browsers as it's setting changes according to various browsers. First look at Firefox so do it according to this.

Go> Page setup> Select Margin & Header footer and make all field blank according to your requirment.
In Chrome after giving print command do following
If you want to hide buttons on printed page then in you javascript code you should hide all buttons after giving print command so do it as
 <script type="text/javascript">
        function printpage() {
            var a = document.getElementById("Button1");
            var b = document.getElementById("Button2");
            a.style.visibility = 'hidden';
            b.style.visibility = 'hidden';
            window.print()
        }
</script>
And in your button give command like this..
<asp:Button class="button" ID="Button1" runat="server" Height="33px" Text="Print"
                        Width="117px" OnClientClick="printpage()"  />

In the above javascript code Button2 is extra button in my page that's why it is in the code.

hope this will help ..

Pass Value From One Web Page To Another in Asp.Net

Suppose there is a scenario in which you want when you click on some text then associated value should pass to another page and with the help of this associated value you can do anything. That link my be simply in paragraph or in Grid View or in Data list.
There may be many methods to do it and I hope one of them called 'Session' be in you knowledge. But doing this with help of session is not considered good so here we will use other technique as well..
Suppose you have Page 'a.aspx' and other page 'b.aspx' you want to pass value from one to another then on first page write this code but before this suppose you have a link button on click of this associated value will pass to 'b.aspx'.
code on click event of link button in 'a.aspx'

Response.Redirect("b.aspx?value=" + here you should write variable of reference variable);
Response.Redirect("b.aspx");

on the page 'b.aspx' of page load event write following code (you can take this value in variable in the form of string.

string passedvalue= Request.QueryString["value"];
here value will carry the assigned value from one page to another. But if you are using Grid View and have link and want to display associated data on other page. Then you will have to use OnRowCommand="shashi_RowCommand" event of Grid View and corresponding command name property of link button so that various link button associated with Grid View can perform various tasks.
In my example.

here i have two link buttons in my grid view that's why used two command name so that they can perform various task associated with them. You will have to give command name in link button. see this image.