viernes, 11 de noviembre de 2016

Producto.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Productos.aspx.cs" Inherits="Productos" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   


        PRODUCTOID:
        <asp:TextBox ID="TextBox1" runat="server" Width="80px"></asp:TextBox>
        <br />
        NOMBRE:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        DESCRIPCION:<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Height="36px" Width="162px"></asp:TextBox>
        <br />
        PRECIO:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <br />
   


        <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

Producto.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Productos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = GetDatos();
        GridView1.DataBind();
    }

    public DataTable GetDatos()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source = SQL5019.SmarterASP.NET;
                                Initial Catalog = DB_9D6D62_aspnet;
                                User Id = DB_9D6D62_aspnet_admin;
                                Password = Aspnet123*";

        SqlDataAdapter da = new SqlDataAdapter(@"select * from productos
                                                order by nombre", con);

        DataTable dt = new DataTable();

        //Llenando el data table
        da.Fill(dt);

        return dt;
    }

    public void InsertDatos(int productoId, string nombre,
                            string desc, decimal precio)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source = SQL5019.SmarterASP.NET;
                                Initial Catalog = DB_9D6D62_aspnet;
                                User Id = DB_9D6D62_aspnet_admin;
                                Password = Aspnet123*";

        SqlDataAdapter da = new SqlDataAdapter(@"insert productos
                                                 select " + productoId.ToString() + ","+
                                                            "'"+nombre + "'," +
                                                            "'" + desc +"',"+
                                                            precio.ToString(),
                                               con);

        //Llenando el data table
        da.SelectCommand.Connection.Open();
        da.SelectCommand.ExecuteNonQuery();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int _productoId = Convert.ToInt32(TextBox1.Text);
        string _nombre = TextBox2.Text;
        string _descripcion = TextBox3.Text;
        decimal _precio = Convert.ToDecimal(TextBox4.Text);

        InsertDatos(_productoId, _nombre, _descripcion, _precio);

    }
}

URL JTable

http://jtable.org/

Estudiar para la proxima clase

ConnectionString

@"Data Source = SQL5019.SmarterASP.NET;
                                Initial Catalog = DB_9D6D62_aspnet;
                                User Id = DB_9D6D62_aspnet_admin;
                                Password = Aspnet123*"