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.

SQL server tricky questions