This is mi first post.
I´m using Visual Studio to make an Azure application.
I want to do an “Update page”.
this are de steps that i want to implement:
1) The user selects one ID from the DropDownList
2) The user pushes an HTML Button
3) The “System” fill some TextBoxs whith information from a SQL sentence:
Select…where Id= DropDownList1.SelectedValue.ToString()
4) The User can change some information on the TextBoxs and push an ASP Button
5) The System do a SQL UPDATE sentence whith the information of the TextBoxs
I´ve got One DropDownList, IDPRODUCT
<asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="index_Changed" runat="server" ></asp:DropDownList>
I fill it from a SQL sentence on “Page Load”
DropDownList1.Items.Clear();
DropDownList1.Items.Add(" ");
string consultaComboIdCompra = "SELECT DISTINCT IdCompra FROM Compras";
SqlCommand sqlCommandComboIdCompra = new SqlCommand(consultaComboIdCompra, sqlConnection);
sqlConnection.Open();
SqlDataReader readerComboIdCompra = sqlCommandComboIdCompra.ExecuteReader();
if (readerComboIdCompra.HasRows)
{
while (readerComboIdCompra.Read())
{
DropDownList1.Items.Add(readerComboIdCompra.GetString(0));
}
}
sqlConnection.Close();
I´ve one HTML Button, it´s function should be to fill some TextBox with the result of a SQL sentence, like:
string consulta2 = "SELECT Unidades FROM Productos WHERE IdProducto = '" + DropDownList1.SelectedValue.ToString() + "'";
SqlCommand sqlCommand2 = new SqlCommand(consulta2, sqlConnection);
sqlConnection.Open();
SqlDataReader reader2 = sqlCommand2.ExecuteReader();
if (reader2.HasRows)
{
while (reader2.Read())
{
TextBox4.Text = reader2.GetString(0);
}
}
sqlConnection.Close();
For last i´ve implemented the UPDATE sentence
if (IsPostBack)
{
string idCompra = DropDownList1.SelectedValue.ToString();
string consulta3 = "UPDATE Compras SET Unidades = '" + TextBox1.Text + "' WHERE IdCompra = '" + idCompra + "'";
SqlCommand sqlCommand3 = new SqlCommand(consulta3, sqlConnection);
sqlConnection.Open();
SqlDataReader reader3 = sqlCommand3.ExecuteReader();
sqlConnection.Close();
}
I cant “autoanswer”, so i edit my ask
The code that finally works, thanks to nunespascal, is the next one:
public partial class WebFormProductosOpcion5 : System.Web.UI.Page
{
static string strSQLconnection = *********
static SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Clear();
DropDownList1.Items.Add(" ");
string consultaComboIdProducto = "SELECT DISTINCT IdProducto FROM Productos";
SqlCommand sqlCommandComboIdProducto = new SqlCommand(consultaComboIdProducto, sqlConnection);
sqlConnection.Open();
SqlDataReader readerComboIdProducto = sqlCommandComboIdProducto.ExecuteReader();
if (readerComboIdProducto.HasRows)
{
while (readerComboIdProducto.Read())
{
DropDownList1.Items.Add(readerComboIdProducto.GetString(0));
}
}
sqlConnection.Close();
}
}
protected void html_click(object sender, EventArgs e)
{
TextBox2.Text = "HOLA";
Debug.WriteLine("HOLA");
}
protected void HTML_Button_Click(object sender, EventArgs e)
{
string idProductoSeleccionado = DropDownList1.SelectedValue.ToString();
string consultaUltimo = "SELECT * FROM Productos WHERE IdProducto = '" + idProductoSeleccionado + "'";
SqlCommand sqlCommandUltimo = new SqlCommand(consultaUltimo, sqlConnection);
sqlConnection.Open();
SqlDataReader readerUltimo = sqlCommandUltimo.ExecuteReader();
if (readerUltimo.HasRows)
{
while (readerUltimo.Read())
{
//Put the name
TextBox2.Text = readerUltimo.GetString(1);
}
}
sqlConnection.Close();
//Label10.Text = col1;
sqlConnection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
//string consulta = "select * from Productos where IdProducto ='";
//consulta = consulta + idP + "'";
SqlCommand sqlCommand = new SqlCommand(consulta, sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
sqlConnection.Close();
//Mensaje Modificación Correcta
Response.Write("<script language=javascript>alert('Modificado con éxito');</script>");
}
}
You need to bind a
DropDownListonly the first time a user requests the page.Check that the user is not posting back before binding on Page
LoadSince you are updating on a button, you need not check for
IsPostBack. Put that code on the buttonClickevent handler. That way it would run only when someone clicks your update button