viernes, 11 de noviembre de 2016

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);

    }
}

No hay comentarios:

Publicar un comentario