Friday, October 11, 2019

Learning about Templates and Working With Repeater


Templates

In this article, We will learn about templates and also how to make project using Repeater

#Templates

Collection of html and web control. We have total 15 templates.
#The names are as follow:
  1. ItemTemplate
  2. AlternatingTemplate
  3. HeaderTemplate
  4. FooterTemplate
  5. SeparatorTemplate
  6. SelectedItemTemplate
  7. EditItemTemplate
  8. PagerTemplate
  9. EmptyDataTemplate
  10. InsertItemTemplate
  11. LayoutTemplate
  12. GroupTemplate
  13. GroupSeparatorTemplate
  14. ItemSeparatorTemplate
  15. EmptyItemTemplate


Note: All templates are always used with composite data bound control or vice versa.

# Composite Data bound Control

Controls in which inner control, child control or control under control.
There are 7 Composite Data bound Control :
  • Repeater
  • Data list
  • Grid view
  • Detail view
  • Form view
  • List view
  • Data pager

* Repeater

- It is used to display data in single column.
- There is no manipulation (read only).
- No default template
- No paging (not pre-defined paging)
There are 5 templates which apply on Repeater:
ItemTemplate
AlternatingTemplate
HeaderTemplate
FooterTemplate
SeparatorTemplate
# For Repeater
Create new table in vs
Add data in the table

Working With Repeater

 Here we will learn How to work with Repeater

# Open new project in Visual Studio
# File > New project> Language C#

# Add Connection String
* Click on View > Solution explorer > web.config

<configuration>
  <connectionStrings>
    <add name="ms" connectionString="server=DESKTOP-I3R20OL\SQLEXPRESS;database=dbemployee;integrated security= true"/>
  </connectionStrings>
  <system.web>

*Click on website > Add> New item > webform
Click on toolbox> data>repeater
Select a1,a2,a3,a4 and copy then
Click on solution explorer >right click on web app>paste

public partial class WebForm1 : System.Web.UI.Page
    {
        private void Rep_Bind()
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbbook", ConfigurationManager.ConnectionStrings["ms"].ConnectionString);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
          
        }
       

SqlDataAdapter – class of system.data.sqlclient

*SqlDataAdapter= always used with disconnected approach. It implicit open the connection and close the connection on its own.
*Data Set = collection of tables or array of tables. We can set the relation. Read write data and xml format.

# Types of DataSet
  1. Typed dataset
  2. Untyped dataset

*Typed dataset = It defines the structure at design time.
Its extension is .xsd
It have better performance bcoz of early bind concept.
It is mainly used in reports and fixed number of column operation. Ex reports are end result

*Untyped dataset = It defines the structure at runtime, It is late bind concept means memory allocation and runtime data is put at runtime. Therefore slow performance. Column are not fixed.

# Untyped dataset is used :
*Fill method = First open coonection with database then it will execute query. Then it will fetch data and connection will close.

# In page load event
protected void Page_Load(object sender, EventArgs e)
        {
            Rep_Bind();
        }

# Click on webform > Source
Asp: tag prefix for Microsoft
Asp: - repeater tag name
Item template – it runs up to number of records in table
Img src = path for image from table.
<tr> - table row
<td> - column table
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                <ItemTemplate>
                    <table border="1">
                        <tr>
                            <td>
                                <img src ='<%#Eval("bookimg")%>' height="50" width="50" />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>

# To add description to pics

<b> - For bold writing
<br> - For break
</td>
                            <td>
                                <b>Title :</b><%#Eval ("BookTitle") %><br />
                                <b>Author :</b><%#Eval ("BookAuthor") %><br />
                                <b>Publisher :</b><%#Eval ("BookPublisher") %><br />
                                <b>Price :</b><%#Eval ("BookPrice") %><br />
                            </td>



How to make simple project in Visual Studio for Login Check


 For Login Check 

Before we learnt how to make Login Check Store procedure but This article explain How to implement it and  make  Webform for Login check

#Open Visual Studio and make new project then Add new webform
Now design the webform

public partial class WebForm2 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection();

protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
            if (con.State==ConnectionState.Closed)
            {
                con.Open();
            }
        }

# Method to Checkuser (for Reusability)

private Int32 CheckUser(String u, String p)
        {
            SqlCommand cmd = new SqlCommand("LoginCheck", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@un", SqlDbType.VarChar, 50).Value = u;
            cmd.Parameters.Add("@up", SqlDbType.VarChar, 50).Value = p;
            cmd.Parameters.Add("@ret", SqlDbType.Int);
            cmd.Parameters["@ret"].Direction = ParameterDirection.ReturnValue;
            cmd.ExecuteNonQuery();
            Int32 k = Convert.ToInt32(cmd.Parameters["@ret"].Value);
            cmd.Dispose();
            return k;

        }
SqlCommand cmd = new SqlCommand("LoginCheck", con); - 4 of 5
cmd.Parameters.Add("@ret", SqlDbType.Int); - To return value. Return value have only 1 parameter.

# Login button query
Add label on design webform
protected void Button1_Click(object sender, EventArgs e)
        {
            Int32 d = CheckUser(TextBox1.Text, TextBox2.Text);
            if (d==-1)
            {
                Label1.Text = "WrongUser";

            }
            if (d==-2)
            {
                Label1.Text = "WrongPassword";

            }
            if (d==1)
            {
                Label1.Text = "Login";
            }
        }

# Add data in table tbuser(make one)
Open dataconnection>table> show table on tbuser
Add data in it.
Run the program