Saturday, 12 November 2011

How to Print Star in C#

Write the program in C# for print the
                  *
                 **
                ***
               ****
              *****
----------------------------------------
the program is ..
using System;


    class Class1
    {

        static void Main()
        {

            Console.WriteLine("Enter the value for print  the star ");

            int k = int.Parse(Console.ReadLine());

            int n = k - 1;

            int x = 2 * n + 1;

            for (int p = 0; p <= n; p++)
            {

                for (int j = k - 1; j >= 0; j--)
                {

                    Console.Write(" ");

                }

                for (int i = 0; i <= (x - 2 * (k - 1)); i++)
                {

                    if (i % 2 == 1)
                    {

                        Console.Write("*");

                    }

                    else
                    {

                        Console.Write(" ");

                    }

                }

                Console.WriteLine();

                k--;

            }

            Console.ReadLine();

        }

    }
Hope this will help you
Thanks
ROHIT SRIVASTVA




Tuesday, 8 November 2011

How to upload file in Server fron client side

Hi guys some time question arise that how to upload image on server.

there are two technique

1. upload image in sqlserver which is not in actual form it is in byte form .

2. upload image path in SQL Server and image is upload in Server .

here we write the code for upload file in server and path save in sql server

Database structure :
========================================================================
Create Database uploadfile

use uploadfile

Create table upload
(
id int identity(1,1),
imgpath varchar(100)
)

Now Create the Store Procedure ..

Create proc usp_ins_uploadfile
@path varchar(100)
AS
insert into upload values(@path)

========================================================================

Lets start the code open the new website make the folder where u want to save your file or image.i make the folder which name is uploadfile .

below is the design part

<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:FileUpload ID="fupUpload" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>

</div>
</form>

in above there are file upload control and one submit button .
-----------------------------------------------------------------------------
below is the cs code

private string path = HttpContext.Current.Request.PhysicalApplicationPath + "uploadfile";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
#region
//this code is used for the save the image
Random randomno = new Random();
int imageid1 = randomno.Next(0, 1000000);
string strpic = fupUpload.FileName;
if (strpic != "")
{
ViewState["logopath1"] = imageid1.ToString() + fupUpload.FileName.ToString();
fupUpload.PostedFile.SaveAs(path + "/" + ViewState["logopath1"]);
}
else
{
ViewState["logopath1"] = "";
}
#endregion
#region *this code is upload the file path in Server*
int result = 0;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ToString());
SqlCommand cmd = new SqlCommand("usp_ins_uploadfile", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@path", ViewState["logopath1"]);
result = cmd.ExecuteNonQuery();
if (result > 0)
{

}
else
{

}
#endregion
}

Description: In this code i declare the path which have the server path . after that wirte the code in button submit click .
firstly we genrate the random no this is because if file name is same then it override the file or image .
for example we upload image which name is rohit.png then fine this is save into our application folder .
but when again we upload another file or image which name is same like rohit.png then it replace the
previous image or file.in this code i divide the code in two part first region is save the data in folder
and second one is save the path in the database .

Hope this will help you
ROHIT SRIVASTAVA

Friday, 4 November 2011

Computer Magic

Go to google.com and type below line . see what happen 
do a barrel roll
and press enter.
===========================================
2.
Open Microsoft Word and type

=rand (200, 99)

And then press ENTER
Then see the magic...

3.. Go to Goolgle and type TILT and Press enter 
see the magic

hope you have learn something new 
Thanks 
ROHIT SRIVASTVA
 

Saturday, 22 October 2011

Web config file in ASP .net

Description of web config file :
Web.config file  is a configuration file for the Asp .net web application. An Asp .net application has one or more web.config file which keeps the configurations required for the corresponding application. 
Web.config file is written in XML with specific tags having specific meanings.
Some point in Web Config File:
A web Application can contain more than one web config file .and it reside  different sub-directories of your web application. Web config file in sub-directory take precedence over the setting that are specified in the  parent directory .
2. web config file protected by the IIS. so clients can not get to them. if u try to retrive an existing http:hellohome.com/webconfig then u will get Access denied or error .
3.When you modify the settings in the Web.Config file, you do not need to restart the Web service for the modifications to take effect.. By default, the Web.Config file applies to all the pages in the current directory and its subdirectories.
-----------------------------------------------------------------------------------------
The web config is XML file it can consist of any valid xml tags but the root element always be <configuration>

some tag description

1.The <configuration> tag has child tag, which we call section group, the <system.web> tag. A section group typically contains the setting sections, such as: compilation, customErrors, authentication, authorization, etc. The way this works is pretty straightforward: you simply include your settings in the appropriate setting sections. You want to use a different authentication mode for your Web application, you'd change that setting in the authentication section.

2.<authentication>

The authentication section controls the type of authentication used within your Web application, as contained in the attribute mode. You'll enter the value "None" if anyone may access your application. If authentication is required, you'll use "Windows", "Forms" or "Passport" to define the type of authentication. For example:

<authentication mode="Windows" />

3.<authorization>

To allow or deny access to your web application to certain users or roles, use <allow> or <deny> child tags.

<authorization>
<allow roles="Administrators,Users" />
<deny users="*" />
</authorization>

It's important to understand that ASP.NET's authorization module iterates through the sections, applying the first rule that corresponds to the current user. In this example, users carrying the role Administrators or Users will be allowed access, while all others (indicated by the * wildcard) will encounter the second rule and will subsequently be denied access.

4.<compilation>

Here, you can configure the compiler settings for ASP.NET. You can use loads of attributes here, of which the most common are debug and defaultLanguage. Set debug to "true" only if you want the browser to display debugging information. Since turning on this option reduces performance, you'd normally want to set it to "false". The defaultLanguage attribute tells ASP.NET which language compiler to use, since you could use either Visual Basic .NET or C# for instance. It has value vb by default.

5.<customErrors>

To provide your end users with custom, user-friendly error messages, you can set the mode attribute of this section to On. If you set it to RemoteOnly, custom errors will be shown only to remote clients, while local host users will see the ugly but useful ASP.NET errors -- clearly, this is helpful when debugging. Setting the mode attribute to Off will show ASP.NET errors to all users.

If you supply a relative (for instance, /error404.html) or absolute address (http://yourdomain.com/error404.html) in the defaultRedirect attribute, the application will be automatically redirected to this address in case of an error. Note that the relative address is relative to the location of the Web.config file, not the page in which the error takes place. In addition you can use <error> tags to provide a statusCode and a redirect attribute:

<customErrors mode="RemoteOnly" defaultRedirect="/error.html">
<error statusCode="403" redirect="/accessdenied.html" />
<error statusCode="404" redirect="/pagenotfound.html" />
</customErrors>

Friday, 21 October 2011

Print triangle without for loop

  class Program
    {
        static void Main(string[] args)
        {

            int i = 1;
        A: if (i < 7)
            {
                pro(i);
                i++;
                goto A;
            }
            int l = 6;
        D: if (l > 0)
            {
                pro1(l);
                l--;
                goto D;

            }

            Console.Read();
        }

        protected static void pro(int j)
        {
            int k = 1;
        B:
            if (k < j)
            {
                Console.Write(k);

                k++;
                goto B;
            }
            Console.WriteLine();

        }

        protected static void pro1(int j)
        {
            int k = 1;
        C:
            if (k < j)
            {
                Console.Write(k);

                k++;
                goto C;
            }
            Console.WriteLine();

        }
    }

out put is
1
12
123
1234
12345
12345
1234
123
12
1

Thursday, 20 October 2011

What is the Partial Class in C#

Partial Class:

How to find nth highest salary

Question : normally this question asked that write the query for nth highest sal ?
Answer: u use any one of them
1.    select distinct a.salary from Salary a where 5=( select count(distinct b.salary) from Salary b where     a.salary<=b.salary)
------------------------------------------------------
2. Select top 1 salary from (
  
Select Distinct top n salary
    from
employee
    order
by  salary desc) a
    order by
salary

---------------------------------------
3.SELECT distinct salary FROM
(
    SELECT DENSE_RANK() OVER (ORDER BY salary DESC) AS rank, salary
    FROM property_R
) T2
WHERE rank=1 

Wednesday, 19 October 2011

Convert String

my data return string (always 9 character) as following,
000005330
000004110
000041660
how my GridView display above value into decimal. the expected result as following,
53.30
41.10
416.60
------------------------
Sample solution
  string a = "000005330";

            a = a.Insert(a.Length - 2, ".");

            float num = float.Parse(a);

Friday, 14 October 2011

"Diffence between Delete And truncate in SQL Server"

Delete :
1. Delete is DML command  .
2. We can use where condition in Delete.
3. Delete retain the identity.
4. Slower then truncate because it keep logs
5.Triggers get fired in DELETE command
6. DELETE you can rollback data .
7. It deletes specified data if where condition exists
----------------------------------------------------------------------------
Truncatre:
1. Delete is DDL Command.
2. We can not use where condition in truncate .
3. If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column
4. Faster in performance wise, 
5. Triggers not fired in truncate command
6. It cannot rollback data(but it is possible).
7. It delete all data.
----------------------------------------------------------------------------
Question :TRUNCATE is much faster than DELETE.
Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.Thatswhy TRUNCATE is faster.Once you Truncate you cann't get back the data.
----------------------------------------------------------------------------------------------
Question : How to rollback the data when delete command execute?

Answer:
CREATE PROCEDURE [usp_delete_Pataint_master]
 (@PataintID [int],
 @BranchID [int]

)
AS
 BEGIN
 SET NOCOUNT ON
  BEGIN TRAN

  DELETE [dbo].[Pataint_master]
  WHERE ([BranchID] = @BranchID
  AND [PataintID]  = @PataintID)
  IF @@ERROR = 0
   COMMIT TRAN
  ELSE
   ROLLBACK TRAN
  RETURN (@@ERROR)
 END

GO

Saturday, 8 October 2011

When use Array and when use ArrayList


Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array.
ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list of a specific type, ArrayLists fall short because you could pass in an ArrayList containing any type. ArrayLists use a dynamically expanding array internally, so there is also a hit to expand the size of the internal array when it hits its capacity.

Hope this will help you
Thanks
Rohit Srivastava