using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication2
{
public partial class admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015");
// Show data at Display by using gridview----------------------------
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT * FROM userdata";
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
//Response.Write("Can read datasource to Gridview");
/*
// Show and Update data on Display by using gridview----------------------------
con.Open();
adap = new SqlDataAdapter("Select firstname as 'First Name',lastname as 'Last Name',username as 'User Name',password as 'Password' From userdata ",con);
ds = new System.Data.DataSet();
adap.Fill(ds,"userdata");
GridView1.DataSource = ds.Tables[0];
Response.Write("Can read datasource to Gridview");
// End of Show and Update data on Display by using gridview-----------------------
*/
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015";
con.Open();
string deleteCommand = "DELETE userdata WHERE username ='"+TextBoxIDnumber.Text+"'";
SqlCommand cmd = new SqlCommand(deleteCommand, con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Deleted");
}
catch(Exception ex)
{
Response.Write("Error:"+ ex.ToString());
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015";
string updateCommand = "UPDATE userdata SET '"+ DropDownList1.SelectedIndex.ToString()+"' = '" + TextBoxExpression.Text + "' WHERE ID ='" + TextBoxIDnumber.Text + "'";
SqlCommand cmd = new SqlCommand(updateCommand, con);
//"+DropDownList1.SelectedIndex.ToString()+"
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Updated");
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}
}
ASP.NET C# Update Button Error ครับ ช่วยชี้แนะใหน่อย
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication2
{
public partial class admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015");
// Show data at Display by using gridview----------------------------
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT * FROM userdata";
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
//Response.Write("Can read datasource to Gridview");
/*
// Show and Update data on Display by using gridview----------------------------
con.Open();
adap = new SqlDataAdapter("Select firstname as 'First Name',lastname as 'Last Name',username as 'User Name',password as 'Password' From userdata ",con);
ds = new System.Data.DataSet();
adap.Fill(ds,"userdata");
GridView1.DataSource = ds.Tables[0];
Response.Write("Can read datasource to Gridview");
// End of Show and Update data on Display by using gridview-----------------------
*/
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015";
con.Open();
string deleteCommand = "DELETE userdata WHERE username ='"+TextBoxIDnumber.Text+"'";
SqlCommand cmd = new SqlCommand(deleteCommand, con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Deleted");
}
catch(Exception ex)
{
Response.Write("Error:"+ ex.ToString());
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Server=METEE-PC;Database=eAdvice;user id=sa;password=me2015";
string updateCommand = "UPDATE userdata SET '"+ DropDownList1.SelectedIndex.ToString()+"' = '" + TextBoxExpression.Text + "' WHERE ID ='" + TextBoxIDnumber.Text + "'";
SqlCommand cmd = new SqlCommand(updateCommand, con);
//"+DropDownList1.SelectedIndex.ToString()+"
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Updated");
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}
}