using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class alteraCliente : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Label1.Text == String.Empty)
{
string id = Request.QueryString["id"];
string conexao = "Server=localhost;Database=Stonehenge;Uid=root;Pwd=\'\';Connect Timeout=30;";
MySqlConnection conn = new MySqlConnection(conexao);
string comando = "select * from cliente where id=" + id;
MySqlCommand cmd = new MySqlCommand(comando);
cmd.Connection = conn;
conn.Open();
MySqlDataReader myLeitura = cmd.ExecuteReader();
myLeitura.Read();
Label1.Text = myLeitura["id"].ToString();
nome.Text = myLeitura["nome"].ToString();
endereco.Text = myLeitura["endereco"].ToString();
Telefone.Text = myLeitura["telefone"].ToString();
bairro.Text = myLeitura["bairro"].ToString();
Cidade.Text = myLeitura["cidade"].ToString();
estado.SelectedValue = myLeitura["estado"].ToString();
cpf.Text = myLeitura["cpf"].ToString();
myLeitura.Close();
comando = "select * from assuntos where cpf=\\"" + cpf.Text + "\\"";
cmd.CommandText = comando;
MySqlDataReader ass = cmd.ExecuteReader();
while (ass.Read())
{
for (int i = 0; i < assuntos.Items.Count; i++)
{
if (assuntos.Items[i].Text == ass["assunto"].ToString())
{
assuntos.Items.RemoveAt(i);
prediletos.Items.Add(ass["assunto"].ToString());
}
}
}
ass.Close();
conn.Close();
}
}
protected void LtoR_click(object sender, EventArgs e)
{
for (int i = 0; i < assuntos.Items.Count; i++)
{
if (assuntos.Items[i].Selected)
{
prediletos.Items.Add(assuntos.SelectedItem);
assuntos.Items.RemoveAt(assuntos.SelectedIndex);
}
}
}
protected void RtoL_Click(Object sender, EventArgs e)
{
for (int i = 0; i < prediletos.Items.Count; i++)
{
if (prediletos.Items[i].Selected)
{
assuntos.Items.Add(prediletos.SelectedItem);
prediletos.Items.RemoveAt(prediletos.SelectedIndex);
}
}
}
protected void tudoL_Click(object sender, EventArgs e)
{
for (int i = 0; i < prediletos.Items.Count; i++)
{
assuntos.Items.Add(prediletos.Items[i].Text);
}
prediletos.Items.Clear();
}
protected void tudoR_Click(object sender, EventArgs e)
{
for (int i = 0; i < assuntos.Items.Count; i++)
{
prediletos.Items.Add(assuntos.Items[i].Text);
}
assuntos.Items.Clear();
}
protected void btnVoltar_Click(object sender, EventArgs e)
{
Response.Redirect("ListarClientes.aspx");
}
protected void btnSalvar_Click(object sender, EventArgs e)
{
string conexao = "Server=localhost;Database=Stonehenge;Uid=root;Pwd=\'\';Connect Timeout=30;";
MySqlConnection conn = new MySqlConnection(conexao);
string comando = "update cliente set nome=\'"+nome.Text+"\',";
comando += "endereco=\'"+endereco.Text+"\',telefone=\'"+Telefone.Text+"\',";
comando += " bairro=\'" + bairro.Text;
comando += "\', cidade=\'" + Cidade.Text + "\', estado=\'" + estado.SelectedValue + "\',";
comando += "cpf=\'"+cpf.Text+"\' where id="+Label1.Text;
//Response.Write(comando);
conn.Open();
MySqlCommand cmd = new MySqlCommand(comando, conn);
cmd.ExecuteNonQuery();
if (prediletos.Items.Count > 0) {
// deletar e incluir novos registros em assuntos
comando = "delete from assuntos where cpf=" + cpf.Text;
cmd.CommandText = comando;
cmd.ExecuteNonQuery();
for (int i = 0; i <= prediletos.Items.Count; i++)
{
comando = "insert into assuntos (cpf,assunto)";
comando += " values(\'" + cpf.Text + "\',\'" + prediletos.Items[i].Text + "\')";
cmd.CommandText = comando;
cmd.ExecuteNonQuery();
}
}
}
}