Sunday, 17 March 2013

Repeater control Alternative colour for datagrid row


<body>
 
    <form id="form1" runat="server">
 
    <div style="height: 280px">
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:hrdemoConnectionString %>"
            SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
 
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
        <HeaderTemplate>
<h1> Mahalaxmi : Annet</h1>      
        </HeaderTemplate>
        <ItemTemplate>
        <p style="background-color:Yellow"> The id of <%# DataBinder.Eval(Container,"DataItem.name")%> is <%# DataBinder.Eval(Container,"DataItem.id")%> </p>
        </ItemTemplate>
        <AlternatingItemTemplate>
         <p style="background-color:Green"> The id of <%# DataBinder.Eval(Container,"DataItem.name")%> is <%# DataBinder.Eval(Container,"DataItem.id")%> </p>
     
        </AlternatingItemTemplate>
        <FooterTemplate>
        <hr />
        </FooterTemplate>
        </asp:Repeater>
 
    </div>
 
    </form>

Saturday, 16 March 2013

Template field for gridview

1) right click gridview click addnew column
2)choose field type TemplateField give header text name clik on ok
3)after that right click gridview click on edit template link
4)drag label to itemtemplate field
5)click on label edit databinding link
6)Enter Code expression textbox XPath("productname")  click ok button
XPath use for xml file data.

store xml data to dataset


DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/products.xml"));
//Readxml read xml file and convert into datatable
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();

parameterise grid display


   SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand();
        //SqlCommandBuilder c = new SqlCommandBuilder(da);
        da.SelectCommand.CommandText = "select * from emp where id=@p1";
        da.SelectCommand.Connection = con;
        da.SelectCommand.Parameters.Add("@p1", SqlDbType.Int);
        da.SelectCommand.Parameters[0].Value = 1;
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

insert textbox values to the table


  SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into emp values(@p1,@p2)", con);
        cmd.Parameters.Add("@p1", SqlDbType.Int);
        cmd.Parameters.Add("@p2", SqlDbType.VarChar,40);
        cmd.Parameters["@p1"].Value = System.Convert.ToInt32(TextBox1.Text);
        cmd.Parameters[1].Value = TextBox2.Text;
        int ans = cmd.ExecuteNonQuery();
        Response.Write(ans.ToString() + "records inserted");

gridview bind


 SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo") ;
    SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
   

dropdownlist bind


 SqlConnection con = new SqlConnection("Data Source=ANKITA-USER18\\SQL2008R2;Integrated Security=True;Initial Catalog=hrdemo");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from emp", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            String str = dr[0].ToString() + "  :" + dr[1].ToString();
            DropDownList1.Items.Add(str);
        }