Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 44.65 KB | None | 0 0
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Answears.aspx.cs" Inherits="HTML_LAB5_6.Answears" %>
  2.  
  3. <!DOCTYPE html>
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.         <div>
  12.             <h1>Answears</h1>
  13.             <a href="Questions.aspx">Questions</a><br />
  14.             <br />
  15.             <a href="Users.aspx">Users</a><br />
  16.             <br />
  17.             <a href="ChatLogin.aspx">ChatLogin</a><br />
  18.             <br />
  19.             <a href="Chat.aspx">Chat</a><br />
  20.             <br />
  21.             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  22.             <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1">
  23.                 <AlternatingItemTemplate>
  24.                     <tr style="">
  25.                         <td>
  26.                             <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
  27.                         </td>
  28.                         <td>
  29.                             <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
  30.                         </td>
  31.                         <td>
  32.                             <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
  33.                         </td>
  34.                     </tr>
  35.                 </AlternatingItemTemplate>
  36.                 <EditItemTemplate>
  37.                     <tr style="">
  38.                         <td>
  39.                             <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
  40.                             <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
  41.                         </td>
  42.                         <td>
  43.                             <asp:Label ID="IdLabel1" runat="server" Text='<%# Eval("Id") %>' />
  44.                         </td>
  45.                         <td>
  46.                             <asp:TextBox ID="IdPytaniaTextBox" runat="server" Text='<%# Bind("IdPytania") %>' />
  47.                         </td>
  48.                         <td>
  49.                             <asp:TextBox ID="OdpowiedzTextBox" runat="server" Text='<%# Bind("Odpowiedz") %>' />
  50.                         </td>
  51.                     </tr>
  52.                 </EditItemTemplate>
  53.                 <EmptyDataTemplate>
  54.                     <table runat="server" style="">
  55.                         <tr>
  56.                             <td>No data was returned.</td>
  57.                         </tr>
  58.                     </table>
  59.                 </EmptyDataTemplate>
  60.                 <InsertItemTemplate>
  61.                     <tr style="">
  62.                         <td>
  63.                             <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
  64.                             <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
  65.                         </td>
  66.                         <td>
  67.                             <asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' />
  68.                         </td>
  69.                         <td>
  70.                             <asp:TextBox ID="IdPytaniaTextBox" runat="server" Text='<%# Bind("IdPytania") %>' />
  71.                         </td>
  72.                         <td>
  73.                             <asp:TextBox ID="OdpowiedzTextBox" runat="server" Text='<%# Bind("Odpowiedz") %>' />
  74.                         </td>
  75.                     </tr>
  76.                 </InsertItemTemplate>
  77.                 <ItemTemplate>
  78.                     <tr style="">
  79.                         <td>
  80.                             <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
  81.                         </td>
  82.                         <td>
  83.                             <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
  84.                         </td>
  85.                         <td>
  86.                             <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
  87.                         </td>
  88.                     </tr>
  89.                 </ItemTemplate>
  90.                 <LayoutTemplate>
  91.                     <table runat="server">
  92.                         <tr runat="server">
  93.                             <td runat="server">
  94.                                 <table id="itemPlaceholderContainer" runat="server" border="0" style="">
  95.                                     <tr runat="server" style="">
  96.                                         <th runat="server">Id</th>
  97.                                         <th runat="server">IdPytania</th>
  98.                                         <th runat="server">Odpowiedz</th>
  99.                                     </tr>
  100.                                     <tr id="itemPlaceholder" runat="server">
  101.                                     </tr>
  102.                                 </table>
  103.                             </td>
  104.                         </tr>
  105.                         <tr runat="server">
  106.                             <td runat="server" style=""></td>
  107.                         </tr>
  108.                     </table>
  109.                 </LayoutTemplate>
  110.                 <SelectedItemTemplate>
  111.                     <tr style="">
  112.                         <td>
  113.                             <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' />
  114.                         </td>
  115.                         <td>
  116.                             <asp:Label ID="IdPytaniaLabel" runat="server" Text='<%# Eval("IdPytania") %>' />
  117.                         </td>
  118.                         <td>
  119.                             <asp:Label ID="OdpowiedzLabel" runat="server" Text='<%# Eval("Odpowiedz") %>' />
  120.                         </td>
  121.                     </tr>
  122.                 </SelectedItemTemplate>
  123.             </asp:ListView>
  124.             <asp:DataPager ID="ProductListPagerSimple" runat="server"
  125.                 PagedControlID="ListView1" PageSize="5">
  126.                 <Fields>
  127.                     <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
  128.                         ShowLastPageButton="True" />
  129.                 </Fields>
  130.             </asp:DataPager>
  131.             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id], [IdPytania], [Odpowiedz] FROM [Answears] WHERE ([IdPytania] = @IdPytania)">
  132.                 <SelectParameters>
  133.                     <asp:QueryStringParameter Name="IdPytania" QueryStringField="QuestionsID" Type="Int32" />
  134.                 </SelectParameters>
  135.             </asp:SqlDataSource>
  136.             <br />
  137.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  138.             <asp:Button ID="Button1" runat="server" Text="Dodaj odpowiedz" OnClick="Button1_Click" />
  139.         </div>
  140.     </form>
  141. </body>
  142. </html>
  143.  
  144.  
  145.  
  146.  
  147. using System;
  148. using System.Collections.Generic;
  149. using System.Data.SqlClient;
  150. using System.Linq;
  151. using System.Web;
  152. using System.Web.UI;
  153. using System.Web.UI.WebControls;
  154.  
  155. namespace HTML_LAB5_6
  156. {
  157.     public partial class Answears : System.Web.UI.Page
  158.     {
  159.         protected void Page_Load(object sender, EventArgs e)
  160.         {
  161.             Label1.Text = (String)Session["QuestionID"];
  162.         }
  163.  
  164.         protected void Button1_Click(object sender, EventArgs e)
  165.         {
  166.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  167.             con.Open();
  168.             try
  169.             {
  170.                 string sel = "Select count(*) from Answears";
  171.                 SqlCommand scm = new SqlCommand(sel, con);
  172.                 int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
  173.                 sel = "insert into Answears (Id,IdPytania,Odpowiedz) values (@id,@idPytania,@pytanie)";
  174.                 scm = new SqlCommand(sel, con);
  175.                 scm.Parameters.AddWithValue("@id", id);
  176.                 scm.Parameters.AddWithValue("@idPytania", Request.QueryString["QuestionsID"]);
  177.                 scm.Parameters.AddWithValue("@pytanie", TextBox1.Text);
  178.                 scm.ExecuteNonQuery();
  179.                 con.Close();
  180.                 Response.Redirect("Answears.aspx?QuestionsID=" + Request.QueryString["QuestionsID"]);
  181.             }
  182.             catch (Exception ex)
  183.             {
  184.                 Response.Write("Błąd: " + ex.ToString());
  185.                 con.Close();
  186.             }
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192.  
  193. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Chat.aspx.cs" Inherits="HTML_LAB5_6.Chat" %>
  194.  
  195. <!DOCTYPE html>
  196.  
  197. <html xmlns="http://www.w3.org/1999/xhtml">
  198. <head runat="server">
  199.     <title></title>
  200. </head>
  201. <body>
  202.     <form id="form1" runat="server">
  203.         <div>
  204.             <h1>Chat</h1>
  205.             <a href="Questions.aspx">Questions</a><br />
  206.             <br />
  207.             <a href="Answears.aspx">Answears</a><br />
  208.             <br />
  209.             <a href="Users.aspx">Users</a><br />
  210.             <br />
  211.             <a href="ChatLogin.aspx">ChatLogin</a><br />
  212.             <br />
  213.             <h3>
  214.                 <asp:Label ID="Label1" runat="server" Text=""></asp:Label></h3>
  215.             <br />
  216.             <br />
  217.             <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="MyListView_ItemCommand">
  218.                 <AlternatingItemTemplate>
  219.                     <li style="background-color: #FFF8DC;">User:
  220.                         <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
  221.                         <br />
  222.                         Message:
  223.                         <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
  224.                         <br />
  225.                         <asp:Button ID="ButtonRemove" CommandName="Remove" Text="Usun" CommandArgument='<%# Eval("IdMsg") %>' Enabled='<%# Eval("Userr").ToString() == (String)Session["ChatUser"] %>' runat="server" />
  226.                     </li>
  227.                 </AlternatingItemTemplate>
  228.                 <EditItemTemplate>
  229.                     <li style="background-color: #008A8C; color: #FFFFFF;">User:
  230.                         <asp:TextBox ID="UserTextBox" runat="server" Text='<%# Bind("Userr") %>' />
  231.                         <br />
  232.                         Message:
  233.                         <asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' />
  234.                         <br />
  235.                         <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
  236.                         <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
  237.                     </li>
  238.                 </EditItemTemplate>
  239.                 <EmptyDataTemplate>
  240.                     No data was returned.
  241.                 </EmptyDataTemplate>
  242.                 <InsertItemTemplate>
  243.                     <li style="">User:
  244.                         <asp:TextBox ID="UserTextBox" runat="server" Text='<%# Bind("Userr") %>' />
  245.                         <br />
  246.                         Message:
  247.                         <asp:TextBox ID="MessageTextBox" runat="server" Text='<%# Bind("Message") %>' />
  248.                         <br />
  249.                         <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
  250.                         <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
  251.                     </li>
  252.                 </InsertItemTemplate>
  253.                 <ItemSeparatorTemplate>
  254.                     <br />
  255.                 </ItemSeparatorTemplate>
  256.                 <ItemTemplate>
  257.                     <li style="background-color: #DCDCDC; color: #000000;">User:
  258.                         <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
  259.                         <br />
  260.                         Message:
  261.                         <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
  262.                         <br />
  263.                         <asp:Button ID="ButtonRemove" CommandName="Remove" Text="Usun" CommandArgument='<%# Eval("IdMsg") %>' Enabled='<%# Eval("Userr").ToString() == (String)Session["ChatUser"] %>' runat="server" />
  264.                     </li>
  265.                 </ItemTemplate>
  266.                 <LayoutTemplate>
  267.                     <ul id="itemPlaceholderContainer" runat="server" style="font-family: Verdana, Arial, Helvetica, sans-serif;">
  268.                         <li runat="server" id="itemPlaceholder" />
  269.                     </ul>
  270.                     <div style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;">
  271.                     </div>
  272.                 </LayoutTemplate>
  273.                 <SelectedItemTemplate>
  274.                     <li style="background-color: #008A8C; font-weight: bold; color: #FFFFFF;">User:
  275.                         <asp:Label ID="UserLabel" runat="server" Text='<%# Eval("Userr") %>' />
  276.                         <br />
  277.                         Message:
  278.                         <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' />
  279.                         <br />
  280.                     </li>
  281.                 </SelectedItemTemplate>
  282.             </asp:ListView>
  283.             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT TOP 5 [IdMsg], [Userr], [Message] FROM [ChatMsg] ORDER BY [IdMsg] DESC"></asp:SqlDataSource>
  284.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  285.             <asp:Button ID="Button1" runat="server" Text="Wyslij" OnClick="Button1_Click" />
  286.         </div>
  287.     </form>
  288. </body>
  289. </html>
  290.  
  291.  
  292.  
  293. using System;
  294. using System.Collections.Generic;
  295. using System.Data.SqlClient;
  296. using System.Linq;
  297. using System.Web;
  298. using System.Web.UI;
  299. using System.Web.UI.WebControls;
  300.  
  301. namespace HTML_LAB5_6
  302. {
  303.     public partial class Chat : System.Web.UI.Page
  304.     {
  305.         protected void Page_Load(object sender, EventArgs e)
  306.         {
  307.             Label1.Text = "Witaj "+(String)Session["ChatUser"]+" na chacie!";
  308.         }
  309.  
  310.         protected void Button1_Click(object sender, EventArgs e)
  311.         {
  312.             String msg = TextBox1.Text;
  313.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  314.             con.Open();
  315.             try
  316.             {
  317.                 string sel = "SELECT top 1 IdMsg from ChatMsg ORDER BY IdMsg DESC";
  318.                 SqlCommand scm = new SqlCommand(sel, con);
  319.                 int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
  320.                 id++;
  321.                 sel = "insert into ChatMsg (IdMsg,Userr,Message) values (@id,@user,@message)";
  322.                 scm = new SqlCommand(sel, con);
  323.                 scm.Parameters.AddWithValue("@id", id);
  324.                 scm.Parameters.AddWithValue("@user", (String)Session["ChatUser"]);
  325.                 scm.Parameters.AddWithValue("@message", TextBox1.Text);
  326.                 scm.ExecuteNonQuery();
  327.                 con.Close();
  328.                 Response.Redirect("Chat.aspx");
  329.             }
  330.             catch (Exception ex)
  331.             {
  332.                 Response.Write("Błąd: " + ex.ToString());
  333.                 con.Close();
  334.             }
  335.         }
  336.  
  337.         protected void MyListView_ItemCommand(object sender, CommandEventArgs e)
  338.         {
  339.             int id;
  340.             //Response.Write("<script>alert('Dziala')</script>");
  341.            
  342.             if (!int.TryParse((string)e.CommandArgument, out id))
  343.             {
  344.                 Response.Write(id);
  345.                 // log.Write("possible sql injection");
  346.                 return;
  347.             }
  348.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  349.             con.Open();
  350.             try
  351.             {
  352.                 string sel = "delete from ChatMsg where IdMsg = '"+id+"'";
  353.                 SqlCommand scm = new SqlCommand(sel, con);
  354.                 scm.ExecuteNonQuery();
  355.                
  356.                 con.Close();
  357.                 Response.Redirect("Chat.aspx");
  358.             }
  359.             catch (Exception ex)
  360.             {
  361.                 Response.Write("Błąd: " + ex.ToString());
  362.                 con.Close();
  363.             }
  364.         }
  365.     }
  366. }
  367.  
  368.  
  369. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChatLogin.aspx.cs" Inherits="HTML_LAB5_6.ChatLogin" %>
  370.  
  371. <!DOCTYPE html>
  372.  
  373. <html xmlns="http://www.w3.org/1999/xhtml">
  374. <head runat="server">
  375.     <title></title>
  376. </head>
  377. <body>
  378.     <form id="form1" runat="server">
  379.         <div>
  380.             <h1>ChatLogin</h1>
  381.             <a href="Questions.aspx">Questions</a><br />
  382.             <br />
  383.             <a href="Answears.aspx">Answears</a><br />
  384.             <br />
  385.             <a href="Users.aspx">Users</a><br />
  386.             <br />
  387.             <asp:Label ID="Label1" runat="server" Text="Wpisz nazwe uzytkownika:"></asp:Label><br />
  388.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
  389.             <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
  390.             <br />
  391.         </div>
  392.     </form>
  393. </body>
  394. </html>
  395.  
  396.  
  397.  
  398. using System;
  399. using System.Collections.Generic;
  400. using System.Data.SqlClient;
  401. using System.Linq;
  402. using System.Web;
  403. using System.Web.UI;
  404. using System.Web.UI.WebControls;
  405.  
  406. namespace HTML_LAB5_6
  407. {
  408.     public partial class ChatLogin : System.Web.UI.Page
  409.     {
  410.         protected void Page_Load(object sender, EventArgs e)
  411.         {
  412.  
  413.         }
  414.  
  415.         protected void Button1_Click(object sender, EventArgs e)
  416.         {
  417.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  418.             con.Open();
  419.             try
  420.             {
  421.                 string sel = "Select count(*) from ChatUsers where Userr = '"+TextBox1.Text+"'";
  422.                 SqlCommand scm = new SqlCommand(sel, con);
  423.                 int user = Convert.ToInt32(scm.ExecuteScalar().ToString());
  424.                 if (user == 0)
  425.                 {
  426.                     sel = "insert into ChatUsers (Userr) values (@user)";
  427.                     scm = new SqlCommand(sel, con);
  428.                     scm.Parameters.AddWithValue("@user", TextBox1.Text);
  429.                     scm.ExecuteNonQuery();
  430.                    
  431.                     Session["ChatUser"]= TextBox1.Text;
  432.                 }else
  433.                 {
  434.                     Session["ChatUser"] = TextBox1.Text;
  435.                 }
  436.                 con.Close();
  437.                 Response.Redirect("Chat.aspx");
  438.             }
  439.             catch (Exception ex)
  440.             {
  441.                 Response.Write("Błąd: " + ex.ToString());
  442.                 con.Close();
  443.             }
  444.         }
  445.     }
  446. }
  447.  
  448.  
  449.  
  450. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Questions.aspx.cs" Inherits="HTML_LAB5_6.Questions" %>
  451.  
  452. <!DOCTYPE html>
  453.  
  454. <html xmlns="http://www.w3.org/1999/xhtml">
  455. <head runat="server">
  456.     <title></title>
  457. </head>
  458. <body>
  459.     <form id="form1" runat="server">
  460.         <div>
  461.             <h1>Questions</h1>
  462.             <a href="Answears.aspx">Answears</a><br />
  463.             <br />
  464.             <a href="Users.aspx">Users</a><br />
  465.             <br />
  466.             <a href="ChatLogin.aspx">ChatLogin</a><br />
  467.             <br />
  468.             <a href="Chat.aspx">Chat</a><br />
  469.             <br />
  470.             <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="IdPytania" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageSize="5" Width="558px" >
  471.                 <Columns>
  472.                     <asp:CommandField ShowSelectButton="True"/>
  473.                     <asp:BoundField DataField="IdPytania" HeaderText="IdPytania" ReadOnly="True" SortExpression="IdPytania" />
  474.                     <asp:BoundField DataField="Pytanie" HeaderText="Pytanie" SortExpression="Pytanie" />
  475.                 </Columns>
  476.             </asp:GridView>
  477.             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Questions]"></asp:SqlDataSource>
  478.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  479.             <asp:Button ID="Button1" runat="server" Text="Dodaj pytanie" OnClick="Button1_Click" />
  480.  
  481.         </div>
  482.     </form>
  483. </body>
  484. </html>
  485.  
  486.  
  487.  
  488. using System;
  489. using System.Collections.Generic;
  490. using System.Data;
  491. using System.Data.SqlClient;
  492. using System.Linq;
  493. using System.Web;
  494. using System.Web.UI;
  495. using System.Web.UI.WebControls;
  496.  
  497. namespace HTML_LAB5_6
  498. {
  499.     public partial class Questions : System.Web.UI.Page
  500.     {
  501.         protected void Page_Load(object sender, EventArgs e)
  502.         {
  503.  
  504.         }
  505.  
  506.         protected void Button1_Click(object sender, EventArgs e)
  507.         {
  508.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  509.             con.Open();
  510.             try
  511.             {
  512.                 string sel = "Select count(*) from Questions";
  513.                 SqlCommand scm = new SqlCommand(sel, con);
  514.                 int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
  515.                 sel = "insert into Questions (IdPytania,Pytanie) values (@id,@pytanie)";
  516.                 scm = new SqlCommand(sel, con);
  517.                 scm.Parameters.AddWithValue("@id", id);
  518.                 scm.Parameters.AddWithValue("@pytanie", TextBox1.Text);
  519.                 scm.ExecuteNonQuery();
  520.                 con.Close();
  521.                 Response.Redirect("Questions.aspx");
  522.             }
  523.             catch (Exception ex)
  524.             {
  525.                 Response.Write("Błąd: " + ex.ToString());
  526.                 con.Close();
  527.             }
  528.         }
  529.  
  530.         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  531.         {
  532.             string id = GridView1.SelectedRow.Cells[1].Text;
  533.             string pytanie = GridView1.SelectedRow.Cells[2].Text;
  534.             Session["QuestionID"] = pytanie;
  535.             Response.Redirect("~/Answears.aspx?QuestionsID="+id);
  536.         }
  537.  
  538.         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  539.         {
  540.             if (e.Row.RowType == DataControlRowType.DataRow)
  541.             {
  542.                 e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
  543.                 e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
  544.                 e.Row.ToolTip = "Click last column for selecting this row.";
  545.             }
  546.         }
  547.     }
  548. }
  549.  
  550.  
  551.  
  552. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Users.aspx.cs" Inherits="HTML_LAB5_6.Users" %>
  553.  
  554. <!DOCTYPE html>
  555.  
  556. <html xmlns="http://www.w3.org/1999/xhtml">
  557. <head runat="server">
  558.     <title></title>
  559. </head>
  560. <body>
  561.     <form id="form1" runat="server">
  562.         <div>
  563.             <h1>Users</h1>
  564.             <a href="Questions.aspx">Questions</a><br />
  565.             <br />
  566.             <a href="Answears.aspx">Answears</a><br />
  567.             <br />
  568.             <a href="ChatLogin.aspx">ChatLogin</a><br />
  569.             <br />
  570.             <a href="Chat.aspx">Chat</a><br />
  571.             <br />
  572.             <asp:ScriptManager ID="MainScriptManager" runat="server" />
  573.             <asp:UpdatePanel ID="pnlHelloWorld" runat="server">
  574.                 <ContentTemplate>
  575.                     <div>
  576.                         <asp:Repeater ID="Repeater1" runat="server" >
  577.                             <HeaderTemplate>
  578.                                 <table>
  579.                                     <tr>
  580.                                         <th>
  581.                                             <asp:LinkButton ID="LinkButton1" runat="server" Text='Id' OnCommand="Sort_Id"
  582.                                                 CommandName="Id" CommandArgument="Id" CausesValidation="false"></asp:LinkButton>
  583.                                         </th>
  584.                                         <th>
  585.                                             <asp:LinkButton ID="SortButtonImie" runat="server" Text='Imie' OnCommand="Sort_Id"
  586.                                                 CommandName="Imie" CommandArgument="Imie" CausesValidation="false"></asp:LinkButton>
  587.                                         </th>
  588.                                         <th>
  589.                                             <asp:LinkButton ID="LinkButton3" runat="server" Text='Nazwisko' OnCommand="Sort_Id"
  590.                                                 CommandName="Nazwisko" CommandArgument="Nazwisko" CausesValidation="false"></asp:LinkButton>
  591.                                         </th>
  592.                                         <th>
  593.                                             <asp:LinkButton ID="LinkButton4" runat="server" Text='Data Urodzenia' OnCommand="Sort_Id"
  594.                                                 CommandName="DataUrodzenia" CommandArgument="DataUrodzenia" CausesValidation="false"></asp:LinkButton>
  595.                                         </th>
  596.                                         <th>
  597.                                             <asp:LinkButton ID="LinkButton5" runat="server" Text='Administrator' OnCommand="Sort_Id"
  598.                                                 CommandName="Administrator" CommandArgument="Administrator" CausesValidation="false"></asp:LinkButton>
  599.                                         </th>
  600.                                     </tr>
  601.                             </HeaderTemplate>
  602.                             <ItemTemplate>
  603.                                 <tr>
  604.                                     <td>
  605.                                         <asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Id")  %>' OnCommand="LinkButton2_Command"
  606.                                             CommandName="Edit" CommandArgument='<%# Eval("Id") %>' CausesValidation="false"></asp:LinkButton>
  607.                                     </td>
  608.                                     <td>
  609.                                         <asp:Label ID="ImieR" runat="server" Text='<%# Eval("Imie")  %>' />
  610.                                     </td>
  611.                                     <td>
  612.                                         <asp:Label ID="NazwiskoR" runat="server" Text='<%# Eval("Nazwisko")  %>' />
  613.                                     </td>
  614.                                     <td>
  615.                                         <asp:Label ID="DataUrodzeniaR" runat="server" Text='<%# Eval("DataUrodzenia")  %>' />
  616.                                     </td>
  617.                                     <td>
  618.                                         <asp:Label ID="CommentTextBoxR" runat="server" Text='<%# Eval("Administrator")  %>' />
  619.                                     </td>
  620.                                 </tr>
  621.                             </ItemTemplate>
  622.                             <FooterTemplate>
  623.                                 </table>
  624.                             </FooterTemplate>
  625.                         </asp:Repeater>
  626.                     </div>
  627.                     <div style="margin-top: 20px;">
  628.                         <table style="width: 600px;">
  629.                             <tr>
  630.                                 <td>
  631.                                     <asp:LinkButton ID="lbFirst" runat="server"
  632.                                         OnClick="lbFirst_Click">First</asp:LinkButton>
  633.                                 </td>
  634.                                 <td>
  635.                                     <asp:LinkButton ID="lbPrevious" runat="server"
  636.                                         OnClick="lbPrevious_Click">Previous</asp:LinkButton>
  637.                                 </td>
  638.                                 <td>
  639.                                     <asp:DataList ID="rptPaging" runat="server"
  640.                                         OnItemCommand="rptPaging_ItemCommand"
  641.                                         OnItemDataBound="rptPaging_ItemDataBound"
  642.                                         RepeatDirection="Horizontal">
  643.                                         <ItemTemplate>
  644.                                             <asp:LinkButton ID="lbPaging" runat="server"
  645.                                                 CommandArgument='<%# Eval("PageIndex") %>'
  646.                                                 CommandName="newPage"
  647.                                                 Text='<%# Eval("PageText") %> ' Width="20px">
  648.                                             </asp:LinkButton>
  649.                                         </ItemTemplate>
  650.                                     </asp:DataList>
  651.                                 </td>
  652.                                 <td>
  653.                                     <asp:LinkButton ID="lbNext" runat="server"
  654.                                         OnClick="lbNext_Click">Next</asp:LinkButton>
  655.                                 </td>
  656.                                 <td>
  657.                                     <asp:LinkButton ID="lbLast" runat="server"
  658.                                         OnClick="lbLast_Click">Last</asp:LinkButton>
  659.                                 </td>
  660.                                 <td>
  661.                                     <asp:Label ID="lblpage" runat="server" Text=""></asp:Label>
  662.                                 </td>
  663.                             </tr>
  664.                         </table>
  665.  
  666.                     </div>
  667.                 </ContentTemplate>
  668.             </asp:UpdatePanel>
  669.             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id], [Imie], [Nazwisko], [DataUrodzenia], [Administrator] FROM [Users]"></asp:SqlDataSource>
  670.             <br />
  671.             <br />
  672.             <asp:Label ID="Label1" runat="server" Text="Imie"></asp:Label>
  673.             <asp:TextBox ID="Imie" runat="server"></asp:TextBox>
  674.             <br />
  675.             <asp:Label ID="Label2" runat="server" Text="Nazwisko"></asp:Label>
  676.             <asp:TextBox ID="Nazwisko" runat="server"></asp:TextBox>
  677.             <br />
  678.             <asp:Label ID="Label3" runat="server" Text="DataUrodzenia"></asp:Label>
  679.             <asp:TextBox ID="DataUrodzenia" runat="server"></asp:TextBox>
  680.             <br />
  681.             <asp:Label ID="Label4" runat="server" Text="Administrator"></asp:Label>
  682.             <asp:RadioButton ID="RadioButton1" runat="server" />
  683.             <br />
  684.             <asp:Button ID="Button1" runat="server" Text="Dodaj uzytkownika" OnClick="Button1_Click" />
  685.             <br />
  686.         </div>
  687.  
  688.     </form>
  689. </body>
  690. </html>
  691.  
  692.  
  693. using System;
  694. using System.Collections.Generic;
  695. using System.Data;
  696. using System.Data.SqlClient;
  697. using System.Drawing;
  698. using System.Linq;
  699. using System.Web;
  700. using System.Web.UI;
  701. using System.Web.UI.WebControls;
  702.  
  703. namespace HTML_LAB5_6
  704. {
  705.     public partial class Users : System.Web.UI.Page
  706.     {
  707.         readonly PagedDataSource _pgsource = new PagedDataSource();
  708.         int _firstIndex, _lastIndex;
  709.         private int _pageSize = 5;
  710.         private int CurrentPage
  711.         {
  712.             get
  713.             {
  714.                 if (ViewState["CurrentPage"] == null)
  715.                 {
  716.                     return 0;
  717.                 }
  718.                 return ((int)ViewState["CurrentPage"]);
  719.             }
  720.             set
  721.             {
  722.                 ViewState["CurrentPage"] = value;
  723.             }
  724.         }
  725.  
  726.         protected void Page_Load(object sender, EventArgs e)
  727.         {
  728.             if (Page.IsPostBack) return;
  729.             BindDataIntoRepeater();
  730.         }
  731.  
  732.         // Get data from database/repository
  733.         static DataTable GetDataFromDb()
  734.         {
  735.             var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  736.             con.Open();
  737.             var da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
  738.             var dt = new DataTable();
  739.             da.Fill(dt);
  740.             con.Close();
  741.             return dt;
  742.         }
  743.  
  744.         // Bind PagedDataSource into Repeater
  745.         private void BindDataIntoRepeater()
  746.         {
  747.             var dt = GetDataFromDb();
  748.             _pgsource.DataSource = dt.DefaultView;
  749.             _pgsource.AllowPaging = true;
  750.             // Number of items to be displayed in the Repeater
  751.             _pgsource.PageSize = _pageSize;
  752.             _pgsource.CurrentPageIndex = CurrentPage;
  753.             // Keep the Total pages in View State
  754.             ViewState["TotalPages"] = _pgsource.PageCount;
  755.             // Example: "Page 1 of 10"
  756.             lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
  757.             // Enable First, Last, Previous, Next buttons
  758.             lbPrevious.Enabled = !_pgsource.IsFirstPage;
  759.             lbNext.Enabled = !_pgsource.IsLastPage;
  760.             lbFirst.Enabled = !_pgsource.IsFirstPage;
  761.             lbLast.Enabled = !_pgsource.IsLastPage;
  762.  
  763.             // Bind data into repeater
  764.             Repeater1.DataSource = _pgsource;
  765.             Repeater1.DataBind();
  766.  
  767.             // Call the function to do paging
  768.             HandlePaging();
  769.         }
  770.  
  771.         private void HandlePaging()
  772.         {
  773.             var dt = new DataTable();
  774.             dt.Columns.Add("PageIndex"); //Start from 0
  775.             dt.Columns.Add("PageText"); //Start from 1
  776.  
  777.             _firstIndex = CurrentPage - 5;
  778.             if (CurrentPage > 5)
  779.                 _lastIndex = CurrentPage + 5;
  780.             else
  781.                 _lastIndex = 10;
  782.  
  783.             // Check last page is greater than total page then reduced it
  784.             // to total no. of page is last index
  785.             if (_lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
  786.             {
  787.                 _lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
  788.                 _firstIndex = _lastIndex - 10;
  789.             }
  790.  
  791.             if (_firstIndex < 0)
  792.                 _firstIndex = 0;
  793.  
  794.             // Now creating page number based on above first and last page index
  795.             for (var i = _firstIndex; i < _lastIndex; i++)
  796.             {
  797.                 var dr = dt.NewRow();
  798.                 dr[0] = i;
  799.                 dr[1] = i + 1;
  800.                 dt.Rows.Add(dr);
  801.             }
  802.  
  803.             rptPaging.DataSource = dt;
  804.             rptPaging.DataBind();
  805.         }
  806.  
  807.         protected void lbFirst_Click(object sender, EventArgs e)
  808.         {
  809.             CurrentPage = 0;
  810.             BindDataIntoRepeater();
  811.         }
  812.         protected void lbLast_Click(object sender, EventArgs e)
  813.         {
  814.             CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
  815.             BindDataIntoRepeater();
  816.         }
  817.         protected void lbPrevious_Click(object sender, EventArgs e)
  818.         {
  819.             CurrentPage -= 1;
  820.             BindDataIntoRepeater();
  821.         }
  822.         protected void lbNext_Click(object sender, EventArgs e)
  823.         {
  824.             CurrentPage += 1;
  825.             BindDataIntoRepeater();
  826.         }
  827.  
  828.         protected void rptPaging_ItemCommand(object source, DataListCommandEventArgs e)
  829.         {
  830.             if (!e.CommandName.Equals("newPage")) return;
  831.             CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
  832.             BindDataIntoRepeater();
  833.         }
  834.  
  835.         protected void rptPaging_ItemDataBound(object sender, DataListItemEventArgs e)
  836.         {
  837.             var lnkPage = (LinkButton)e.Item.FindControl("lbPaging");
  838.             if (lnkPage.CommandArgument != CurrentPage.ToString()) return;
  839.             lnkPage.Enabled = false;
  840.             lnkPage.BackColor = Color.FromName("#00FF00");
  841.         }
  842.  
  843.         protected void LinkButton2_Command(object sender, CommandEventArgs e)
  844.         {
  845.             Response.Redirect("Questions.aspx");
  846.         }
  847.  
  848.         protected void Sort_Id(object sender, CommandEventArgs e)
  849.         {
  850.             var con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  851.             con.Open();
  852.             var da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
  853.             var dt = new DataTable();
  854.             switch (e.CommandName)
  855.             {
  856.                 case "Id":
  857.                     if ((String)Session["Sort"] == "DESC")
  858.                     {
  859.                         da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id desc", con);
  860.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Id] DESC";
  861.                         Session["Sort"] = "ASC";
  862.                     }
  863.                     else
  864.                     {
  865.                         da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Id", con);
  866.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Id]";
  867.                         Session["Sort"] = "DESC";
  868.                     }
  869.                     //------------------------------------------------------------------------------//
  870.                     da.Fill(dt);
  871.                     con.Close();
  872.                     _pgsource.DataSource = dt.DefaultView;
  873.                     _pgsource.AllowPaging = true;
  874.                     // Number of items to be displayed in the Repeater
  875.                     _pgsource.PageSize = _pageSize;
  876.                     _pgsource.CurrentPageIndex = CurrentPage;
  877.                     // Keep the Total pages in View State
  878.                     ViewState["TotalPages"] = _pgsource.PageCount;
  879.                     // Example: "Page 1 of 10"
  880.                     lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
  881.                     // Enable First, Last, Previous, Next buttons
  882.                     lbPrevious.Enabled = !_pgsource.IsFirstPage;
  883.                     lbNext.Enabled = !_pgsource.IsLastPage;
  884.                     lbFirst.Enabled = !_pgsource.IsFirstPage;
  885.                     lbLast.Enabled = !_pgsource.IsLastPage;
  886.  
  887.                     // Bind data into repeater
  888.                     Repeater1.DataSource = _pgsource;
  889.                     Repeater1.DataBind();
  890.  
  891.                     // Call the function to do paging
  892.                     HandlePaging();
  893.                     //------------------------------------------------------------------------------//
  894.                     //Repeater1.DataBind();
  895.                     break;
  896.  
  897.                 case "Imie":
  898.                     if ((String)Session["Sort"] == "DESC")
  899.                     {
  900.                         da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Imie desc", con);
  901.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Imie] DESC";
  902.                         Session["Sort"] = "ASC";
  903.                     }
  904.                     else
  905.                     {
  906.                         da = new SqlDataAdapter("Select Id, Imie, Nazwisko, DataUrodzenia, Administrator From Users Order By Imie", con);
  907.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Imie]";
  908.                         Session["Sort"] = "DESC";
  909.                     }
  910.                     //------------------------------------------------------------------------------//
  911.                     da.Fill(dt);
  912.                     con.Close();
  913.                     _pgsource.DataSource = dt.DefaultView;
  914.                     _pgsource.AllowPaging = true;
  915.                     // Number of items to be displayed in the Repeater
  916.                     _pgsource.PageSize = _pageSize;
  917.                     _pgsource.CurrentPageIndex = CurrentPage;
  918.                     // Keep the Total pages in View State
  919.                     ViewState["TotalPages"] = _pgsource.PageCount;
  920.                     // Example: "Page 1 of 10"
  921.                     lblpage.Text = "Page " + (CurrentPage + 1) + " of " + _pgsource.PageCount;
  922.                     // Enable First, Last, Previous, Next buttons
  923.                     lbPrevious.Enabled = !_pgsource.IsFirstPage;
  924.                     lbNext.Enabled = !_pgsource.IsLastPage;
  925.                     lbFirst.Enabled = !_pgsource.IsFirstPage;
  926.                     lbLast.Enabled = !_pgsource.IsLastPage;
  927.  
  928.                     // Bind data into repeater
  929.                     Repeater1.DataSource = _pgsource;
  930.                     Repeater1.DataBind();
  931.  
  932.                     // Call the function to do paging
  933.                     HandlePaging();
  934.                     //------------------------------------------------------------------------------//
  935.                     //Repeater1.DataBind();
  936.                     break;
  937.  
  938.                 case "Nazwisko":
  939.                     if ((String)Session["Sort"] == "DESC")
  940.                     {
  941.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Nazwisko] DESC";
  942.                         Session["Sort"] = "ASC";
  943.                     }
  944.                     else
  945.                     {
  946.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Nazwisko]";
  947.                         Session["Sort"] = "DESC";
  948.                     }
  949.                     Repeater1.DataBind();
  950.                     break;
  951.  
  952.                 case "DataUrodzenia":
  953.                     if ((String)Session["Sort"] == "DESC")
  954.                     {
  955.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [DataUrodzenia] DESC";
  956.                         Session["Sort"] = "ASC";
  957.                     }
  958.                     else
  959.                     {
  960.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [DataUrodzenia]";
  961.                         Session["Sort"] = "DESC";
  962.                     }
  963.                     Repeater1.DataBind();
  964.                     break;
  965.  
  966.                 case "Administrator":
  967.                     if ((String)Session["Sort"] == "DESC")
  968.                     {
  969.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Administrator] DESC";
  970.                         Session["Sort"] = "ASC";
  971.                     }
  972.                     else
  973.                     {
  974.                         //SqlDataSource1.SelectCommand = "SELECT * FROM [Users] ORDER BY [Administrator]";
  975.                         Session["Sort"] = "DESC";
  976.                     }
  977.                     Repeater1.DataBind();
  978.                     break;
  979.  
  980.                 default:
  981.  
  982.                     // The command name is not recognized. Display an error message.
  983.                     //Message.Text = "Command name not recogized.";
  984.                     break;
  985.  
  986.             }
  987.            
  988.         }
  989.  
  990.         protected void Button1_Click(object sender, EventArgs e)
  991.         {
  992.             SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\ja_karol\documents\visual studio 2015\Projects\HTML_LAB5_6\HTML_LAB5_6\App_Data\Database1.mdf;Integrated Security=True");
  993.             con.Open();
  994.             try
  995.             {
  996.                 string sel = "Select count(*) from Users";
  997.                 SqlCommand scm = new SqlCommand(sel, con);
  998.                 int id = Convert.ToInt32(scm.ExecuteScalar().ToString());
  999.                 sel = "insert into Users (Id,Imie,Nazwisko,DataUrodzenia,Administrator) values (@id,@imie,@nazwisko,@dataUrodzenia,@administrator)";
  1000.                 scm = new SqlCommand(sel, con);
  1001.                 scm.Parameters.AddWithValue("@id", id);
  1002.                 scm.Parameters.AddWithValue("@imie", Imie.Text);
  1003.                 scm.Parameters.AddWithValue("@nazwisko", Nazwisko.Text);
  1004.                 scm.Parameters.AddWithValue("@dataUrodzenia", DataUrodzenia.Text);
  1005.                 scm.Parameters.AddWithValue("@administrator", RadioButton1.Checked);
  1006.                 scm.ExecuteNonQuery();
  1007.                 con.Close();
  1008.                 Response.Redirect("Users.aspx");
  1009.             }
  1010.             catch (Exception ex)
  1011.             {
  1012.                 Response.Write("Błąd: " + ex.ToString());
  1013.                 con.Close();
  1014.             }
  1015.         }
  1016.     }
  1017. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement