T
troy
Hey guys - I seriously need some help. got an assignment due tomorrow and
I'm totally stuck. Here's my comment and and I need help with the code to
update the database as well as delete items. Any help is appreciated.
thanks
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace _2870assign2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
public string strConn = ConfigurationSettings.AppSettings["ConnStr"];
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
public string sql = ConfigurationSettings.AppSettings["SQLStrings"];
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Users");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
DataGrid1.DataBind();
Label1.Text = "Opened Connection to " + conn.Database;
Label1.Text = "SQL server version " + conn.ServerVersion;
}
catch(Exception ex)
{
Label1.Text = "Connection failed " + ex.Message;
}
finally
{
conn.Close();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelC
ommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCom
mand_1);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateC
ommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_EditCommand_1(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ID = DataGrid1.DataKeyField.ToString();
///string username = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
//// string password = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
/// Label2.Text = username;
/// Label3.Text = password;
Label4.Text = ID.ToString();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
}
}
I'm totally stuck. Here's my comment and and I need help with the code to
update the database as well as delete items. Any help is appreciated.
thanks
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace _2870assign2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
public string strConn = ConfigurationSettings.AppSettings["ConnStr"];
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
public string sql = ConfigurationSettings.AppSettings["SQLStrings"];
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Users");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
DataGrid1.DataBind();
Label1.Text = "Opened Connection to " + conn.Database;
Label1.Text = "SQL server version " + conn.ServerVersion;
}
catch(Exception ex)
{
Label1.Text = "Connection failed " + ex.Message;
}
finally
{
conn.Close();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelC
ommand);
this.DataGrid1.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCom
mand_1);
this.DataGrid1.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateC
ommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_EditCommand_1(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ID = DataGrid1.DataKeyField.ToString();
///string username = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
//// string password = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
/// Label2.Text = username;
/// Label3.Text = password;
Label4.Text = ID.ToString();
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
}
}