Saturday, 30 November 2013

Bind Value into Dropdown List from Database

1. Open Visual Studio
2. Create a New Website
3. Add web page
4. from WEbsite - >Add New Item -> WebPage- > Click Add.
5. From ToolBox -> Add DropdownList to WebPage.
6. Create StoredProcedure of Select Query.


7. Add namespaces to page

using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;


8. Write Connection String

  static string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    SqlConnection con = new SqlConnection(str);

9. Write Code on Page Load Event

 if (!Page.IsPostBack)
        {
            SqlCommand cmd = new SqlCommand("Select_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = cmd;
            da.Fill(ds);


            if (ds.Tables[0].Rows.Count > 0)
            {
                DropDownList1.DataSource = ds;
                
                DropDownList1.DataTextField = "EmpName";
                DropDownList1.DataValueField = "EmpID";
                DropDownList1.DataBind();
            }
        }

10. Click F5.

Monday, 18 November 2013

Background color of textbox on focus

1. Open Visual Studio
2. Create a New Website
3. Add web page
4. from WEbsite - >Add New Item -> StyleSheet- > Click Add.
5. Write code in the css file



.txt
{

}
.txt:focus
{
    background-color:Red;
}

6. Add css to the web page's head section.

<link rel="stylesheet" type="text/css" href="Styles/Site.css" />

7. Add TextBox to a web page.

  <asp:TextBox ID="txt1" runat="server" CssClass="txt" ></asp:TextBox>

8. Click F5



Monday, 16 September 2013

How to view Data from Database

 1. Open Visual Studio
2. Create a New Website
3 From Database - > Select Stored Procedure -> Right Click - > Add New Stored Procedure.
4. Create Select Query for Employee Table.

5.  Create a method in class for select.


public DataSet Select_Employee()
    {
        SqlCommand cmd = new SqlCommand("Select_Employee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        da.SelectCommand = cmd;
        da.Fill(ds);
        return ds;
    }

6. Add Web Page and Place Gridview.
7. Add namaspace 
using System.Data

8. On Page_Load write Code as below.

 DataSet ds = new DataSet();
    mainClass objmain = new mainClass();
    
    protected void Page_Load(object sender, EventArgs e)
    {

        ds = objmain.Select_Employee();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

9. Click F5.

Sunday, 15 September 2013

How to Insert Data in Database

1. Open Visual Studio
2. Create a New Website
3. From Website - > Add New Item -> SQL Server Database -> Add.
4. From Server Explorer Expand your Database -> Right Click - > Add New Table.



5. From Database - > Select Stored Procedure -> Right Click - > Add New Stored Procedure.
6. Create Insert Query for Employee Table.


7. Add connection string in Web.Config File.

<connectionStrings>
<add name="conn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Asp.net_Controls\App_Data\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>


8. From Website -> Add New Item -> Class -> Give name  -> Click Add.
9. Add namespaces to Class File.

using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;


10. Create a method in class for the insert.

    static string str = WebConfigurationManager.ConnectionStrings["con"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    int i;


    public int insert_Employee(string EmpName, string EmpAddress, string EmpPhone, string EmpMobile,      string EmpSalary, string EmpPassport)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("insert_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmpName", EmpName);
            cmd.Parameters.AddWithValue("@EmpAddress", EmpAddress);
            cmd.Parameters.AddWithValue("@EmpPhone", EmpPhone);
            cmd.Parameters.AddWithValue("@EmpMobile", EmpMobile);
            cmd.Parameters.AddWithValue("@EmpSalary", EmpSalary);
            cmd.Parameters.AddWithValue("@EmpPassport", EmpPassport);
            i = cmd.ExecuteNonQuery();
            return i;
        }
        catch (Exception ex)
        {
            return i = 0;
        }
    }


11. Add web Page. and Design UI.


12. Write Code on Button click event.

 mainClass objMain = new mainClass();
    int i;
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        i = objMain.insert_Employee(txtname.Text, txtadress.Text, txtphone.Text, txtmobile.Text, txtsalry.Text, txtpassport.Text);
        if (i > 0)
        {
            Response.Write("<script language='javascript'>alert('Successfully inserted!');</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('Error try again!');</script>");
        }
    }

13. Click F5.

Tuesday, 10 September 2013

Using SqlDataSource in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create a New Web Page.
4. Place GridView on-page.
5.From GridView Properties Select-> DataSourceID -> New DataSource.



6. Click OK.


7. Click Next.
8. Select Table Name and Columns and click Next.


9. Click Finish.
10. Click F5





Sunday, 8 September 2013

Wizard control in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create New Web Page and Place Wizard control on a page.
4. From Wizard Properties Add WizardSteps.
5. Add Images in Website and Add Code in the .aspx page as below.

<asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="1" Height="165px" 

        Width="211px">
        <WizardSteps>
            <asp:WizardStep runat="server" title="Step 1">
                <img alt="" class="style2" src="rose1.jpg" />
            </asp:WizardStep>
            <asp:WizardStep runat="server" title="Step 2">
            <img alt="" class="style2" src="rose2.jpg" />
            </asp:WizardStep>
            <asp:WizardStep runat="server" Title="Step 3">
            <img alt="" class="style2" src="rose3.jpg" />
            </asp:WizardStep>
            <asp:WizardStep runat="server" Title="Step 4">
            <img alt="" class="style2" src="rose4.jpg" />
            </asp:WizardStep>
        </WizardSteps>

    </asp:Wizard>


6. Click F5.

AdRotator Control in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create New Web Page and Place AdRotator control on a page
4. Add 4 Images in Website like


  • rose1.jpg
  • rose2.jpg
  • rose3.jpg
  • rose4.jpg
5. Add XML File from Website -> Add New Item -> Xml File
6. Write code in XML file as below




<Advertisements>


  <Ad>

    <ImageUrl>rose1.jpg</ImageUrl>

    <NavigateUrl>http://sapnagiyanwani.blogspot.in</NavigateUrl>
    <AlternateText>  Sapna giyanwani </AlternateText>
    <Impressions>20</Impressions>
    <Keyword>Sapna</Keyword>
  </Ad>

  <Ad>
    <ImageUrl>rose2.jpg</ImageUrl>
    <NavigateUrl>http://sapnagiyanwani.blogspot.in</NavigateUrl>
    <AlternateText>Sapna Giyanwani</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>Sapna</Keyword>
  </Ad>

  <Ad>
    <ImageUrl>rose3.jpg</ImageUrl>
    <NavigateUrl>http://sapnagiyanwani.blogspot.in</NavigateUrl>
    <AlternateText>Sapna Giyanwani</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>Sapna</Keyword>
  </Ad>

  <Ad>
    <ImageUrl>rose4.jpg</ImageUrl>
    <NavigateUrl>http://sapnagiyanwani.blogspot.in</NavigateUrl>
    <AlternateText>Sapna giyanwani</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>Sapna</Keyword>
  </Ad>
</Advertisements>


7. Now AdRotator ->Properties -> AdvertisementFile Property -> Set XML File which we have created.

8. Run Website.
9. When you refresh the page every time the image will change.

Creating Stored Procedure Queries

1. Insert Query

ALTER PROCEDURE dbo.insert_Employee


@EmpName nvarchar(50),@EmpAddress nvarchar(50),@EmpPhone nvarchar(50),@EmpMobile nvarchar(50),@EmpSalary nvarchar(50),@EmpPassport nvarchar(50)

AS


insert into Employee values(@EmpName,@EmpAddress,@EmpPhone,@EmpMobile,@EmpSalary,@EmpPassport)


RETURN


2. Select Query

ALTER PROCEDURE dbo.Select_Employee


AS

select * from Employee


RETURN


3. Delete Query


ALTER PROCEDURE dbo.Delete_Employee
@EmpID bigint

AS

delete from Employee where EmpID=@EmpID

RETURN

4. Update Query

ALTER PROCEDURE dbo.update_Employee
@EmpName nvarchar(50),@EmpAddress nvarchar(50),@EmpPhone nvarchar(50),@EmpMobile nvarchar(50),@EmpSalary nvarchar(50),@EmpPassport nvarchar(50),@EmpID bigint

AS

update Employee set EmpName=@EmpName,EmpAddress=@EmpAddress,EmpPhone=@EmpPhone,EmpMobile=@EmpMobile,Empsalary=@EmpSalary,EmpPassport=@EmpPassport
where EmpID=@EmpID

RETURN

Creating Stored Procedure in SQL Server

1.  From View Menu Goto Server Explorer.
2. Expand your Database -> Stored Procedures -> Right Click -> Add New Stored Procedure.
3. You will see the stored procedure as below.

CREATE PROCEDURE dbo.StoredProcedure1

/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
/* SET NOCOUNT ON */

RETURN


Creating Database and Table in Asp.net

1. Open Visual Studio
2. Create a New Website
3. In Solution Explorer Right Click -> Add New Item -> SQL Server Database - > Give Name and Click Add->OK
4. Now From View Menu Goto Server Explorer.
5. Expand your Database -> Tables -> Right Click -> Add New Table.
6. Add Columns in Table as below.




Using Web User Control in Asp.net

1. Open Visual Studio
2. Create a New Website
3. In Solution Explorer Right Click -> Add New Item -> Web User Control - > Give Name and Click Add->OK

Note : Web User Control Extension is .ascx


4. Add Controls to Web User Control as below.




5. Now Add Web Page and from Solution Explorer Drag and Drop Web User Control to Web Page.


Saturday, 7 September 2013

Using Class File in Asp.net

1. Open Visual Studio
2. Create a New Website
3. In Solution Explorer Right Click -> Add New Item -> Class - > Give Name and Click Add->OK
4. Write the Addition method in Class File as below.

public string addition()

    {
        return (10 + 20).ToString();

    }


5. Now Add Web Page to the Site and Add Label.

6. Write code to Page_Load event as below.

 mainClass objMain = new mainClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = objMain.addition();

    }


7. Click F5.

Friday, 6 September 2013

Connection String in Web.config File

1. Open Visual Studio
2. Create a New Website
3. Add connection string in Web.config file

<connectionStrings>

<add name="conn" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Asp.net_Controls\App_Data\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>


4. Use this connection string in the whole application as below.


Add namespace

using System.Web.Configuration;

And store the connection string in the string variable


static string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;

Sitemap in Asp.net

1. Open Visual Studio
2. Create a New Website
3. In Solution Explorer Right Click -> Add New Item -> SiteMap - > Give Name and Click Add.
4. Write code in the Web.sitemap file as below.

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="Default.aspx" title="Home"  description="Home">
        <siteMapNode url="Login.aspx" title="Login"  description="Login" />
        <siteMapNode url="Registration.aspx" title="Registration"  description="Registration" />
    </siteMapNode>

</siteMap>


5. Add New Web Page.

6. Add TreeView on Page.
7. Right-click Treeview -> go to Properties ->Choose DataSourceID -> New Data Source



8. Select the Site Map.
9. Click F5.

Thursday, 5 September 2013

Session in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create 2 Web Pages as below



4. Write the code on Button_Click event

 protected void Button1_Click(object sender, EventArgs e)
    {
        Session["username"] = txtusername.Text;
        Response.Redirect("SessionDisplay.aspx");
    }

5. Add Web Page SessionDisplay.aspx and placeLabel on it.
6. Write code on Page_Load event of SessionDispaly.aspx. you will see result as below image
 if (Session["username"] != null) 
        {
            Label1.Text = Session["username"].ToString();
        }



Wednesday, 4 September 2013

Creating Webservice in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Now Select Solution Explorer -> Add New Item -Web Service -> give Name ->Click Add.

Note: Webservice Extension is .asmx

For Creating Web Service method we need to add  [WebMethod] before method.


5. Now we add the sum of two numbers method in Webservice as below.


     [WebMethod]


    public string addition()

    {
        return (10 + 20).ToString();
    }

6. Save a document.

7. Now Right-click in Solution Explorer and Click Add Web Reference as shown below.



8. You will see Popup as below.


9. Click Web services in this solution you will see the image as below.


10. Click the Webservice link. and Add Web Reference name which is localhost by default. now click Add Reference.



11. Write Code on Page_Load Event

 protected void Page_Load(object sender, EventArgs e)
    {
        localhost.WebService WebRef = new localhost.WebService();
        Response.Write(WebRef.addition());
    }

12. Click F5.






    

SQL server tricky questions