quick issue


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



}




}

}
 
Ad

Advertisements

B

Bruno Jouhier [MVP]

What's the problem?

You cannot just post a piece of code and expect that people are going to:
a) try to figure out what you are trying to do
b) try to figure out what is going wrong
c) fix your problem

You need to give more context. Otherwise nobody's going to help you.
Also, don't post whole files like this, only post small pieces of code that
are relevant to your problem.

Bruno.

troy said:
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();



}




}

}
 
J

Jon Skeet [C# MVP]

Bruno Jouhier said:
What's the problem?

You cannot just post a piece of code and expect that people are going to:
a) try to figure out what you are trying to do
b) try to figure out what is going wrong
c) fix your problem
Agreed.

You need to give more context. Otherwise nobody's going to help you.
Also, don't post whole files like this, only post small pieces of code that
are relevant to your problem.

While I agree that posting huge chunks of code isn't nice (especially
when OE has mangled it so there's an empty line between each line of
code), I have a strong personal preference for whole files. Whole files
can be cut and paste, and then compiled with no further effort.
Snippets take more time. I just like the whole files to still be small
:)
 
T

troy

sorry - didnt mean to offend anybody.

The issue is that I dont know how to update the fields changed in the
datagrid when a user clicks on the edit function. I tried using the
following logic but it gives me an castring error:

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
username = tb.Text;
 
B

Bruno Jouhier [MVP]

While I agree that posting huge chunks of code isn't nice (especially
when OE has mangled it so there's an empty line between each line of
code), I have a strong personal preference for whole files. Whole files
can be cut and paste, and then compiled with no further effort.
Snippets take more time. I just like the whole files to still be small
:)

Yes, but the first thing to do is to describe the problem, with snipplets of
a few lines, because we can often figure out what's wrong by just looking at
these few lines of code. The whole file can be posted afterwards, in a
reply, if the problem is not straightforward. At least, I prefer it this
way.

This style of posting ("I have an urgent assignment, please debug my
program") is just not acceptable.

Bruno.
 
C

C# Learner

Jon Skeet [C# MVP] wrote:

[...]
While I agree that posting huge chunks of code isn't nice (especially
when OE has mangled it so there's an empty line between each line of
code), I have a strong personal preference for whole files. Whole files
can be cut and paste, and then compiled with no further effort.
Snippets take more time. I just like the whole files to still be small
:)

Also, there are special sites for posting source code to ( e.g.
http://sourcepost.sytes.net/ ), where one can post source and have it
syntax-highlighted, etc.

Example posting: http://sourcepost.sytes.net/sourceview.aspx?source_id=14487
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

troy said:
sorry - didnt mean to offend anybody.

The issue is that I dont know how to update the fields changed in the
datagrid when a user clicks on the edit function. I tried using the
following logic but it gives me an castring error:

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
username = tb.Text;

What casting error did you get? Have you tried debugging to find out
what the actual type is?
 
B

Bruno Jouhier [MVP]

Did you try e.Item.Cells[1].Text ?

Bruno.

troy said:
sorry - didnt mean to offend anybody.

The issue is that I dont know how to update the fields changed in the
datagrid when a user clicks on the edit function. I tried using the
following logic but it gives me an castring error:

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
username = tb.Text;
 
Ad

Advertisements

T

troy

Thanks for the responses guys but I got it figured out.


Bruno Jouhier said:
Did you try e.Item.Cells[1].Text ?

Bruno.

troy said:
sorry - didnt mean to offend anybody.

The issue is that I dont know how to update the fields changed in the
datagrid when a user clicks on the edit function. I tried using the
following logic but it gives me an castring error:

// Gets the value the TextBox control in the third column
tb = (TextBox)(e.Item.Cells[1].Controls[0]);
username = tb.Text;


Jon Skeet said:
What's the problem?

You cannot just post a piece of code and expect that people are
going
to:
a) try to figure out what you are trying to do
b) try to figure out what is going wrong
c) fix your problem

Agreed.

You need to give more context. Otherwise nobody's going to help you.
Also, don't post whole files like this, only post small pieces of
code
that
are relevant to your problem.

While I agree that posting huge chunks of code isn't nice (especially
when OE has mangled it so there's an empty line between each line of
code), I have a strong personal preference for whole files. Whole files
can be cut and paste, and then compiled with no further effort.
Snippets take more time. I just like the whole files to still be small
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top