|
-
Dec 19th, 2002, 07:44 AM
#1
Thread Starter
Frenzied Member
Using UPDATE with a DATAGRID problem
Hi I am having problems making the update datagrid property working. It still uses the old value, and I have followed the example in "ASP.NET in three weeks" and it won't work... here is my code:
firs the cs code behind:
PHP Code:
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.Data.OleDb;
namespace WebApplication12
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.DataSet ds;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack )
{
BindData();
}
}
#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.ds = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
//
// ds
//
this.ds.DataSetName = "ds";
this.ds.Locale = new System.Globalization.CultureInfo("sv-SE");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
}
#endregion
public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ds.AcceptChanges();
DataGrid1.DataSource = ds;
BindData();
}
public void BindData()
{
OleDbConnection myConn = new OleDbConnection("Provider=SQLOLEDB;data source=4647117000N26;initial catalog=Northwind;persist security info=False;user id=logger;password=logger;workstation id=WS1321;packet size=4096");
string sqlQuery = "SELECT * FROM Customers";
//DataSet ds = new DataSet();
OleDbDataAdapter objCmd = new OleDbDataAdapter(sqlQuery,myConn);
OleDbCommandBuilder cmdB = new OleDbCommandBuilder(objCmd);
objCmd.Fill(ds, "Customers");
DataGrid1.DataSource = ds;
Page.DataBind();
}
}
}
And then there is my aspx page
PHP Code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication12.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 149px; POSITION: absolute; TOP: 95px" runat="server" Width="627px" Height="428px" AutoGenerateColumns="False" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnCancelCommand="DataGrid1_CancelCommand" DataSource="<%# ds %>">
<AlternatingItemStyle BackColor="#C0C0FF"></AlternatingItemStyle>
<Columns>
<asp:BoundColumn DataField="CustomerID" HeaderText="Kund-ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactName" HeaderText="Namn"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Uppdatera" HeaderText="Ändra" CancelText="Avbryt" EditText="Ändra"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
This is C# syntax, but the pricipal is the same in vb.net.
I am pretty sure I do something wrong in the update event, but I am not sure what to do really. I want to make the changes to my dataset and then use an OLEDBCOMMANDBUILDER object to update the database with the changes made to the local dataset. Then I want to update the datagrid with the new data...
HEEELP!
kind regards
Henrik
-
Dec 19th, 2002, 06:17 PM
#2
Hyperactive Member
slightly different way of doing it but it works:
PHP Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CSharp
{
public class DataGridUpdate : System.Web.UI.Page
{
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
Customers.EditCommand += new DataGridCommandEventHandler(Customers_EditCommand);
}
#endregion
protected DataGrid Customers;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
BindGrid();
}
protected void Customers_EditCommand(object sender, DataGridCommandEventArgs e)
{
Customers.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
protected void Customers_CancelCommand(object sender, DataGridCommandEventArgs e)
{
Customers.EditItemIndex = -1;
BindGrid();
}
protected void Customers_UpdateCommand(object sender, DataGridCommandEventArgs e)
{
string connString = "user id=sa;password=sa;" +
"database=NorthWind;server=DeathAngel;";
SqlConnection cn = new SqlConnection(connString);
string cmdText = "Update Customers Set CompanyName=@CompanyName, " +
"ContactName=@ContactName, ContactTitle=@ContactTitle " +
"Where CustomerID=@CustomerID";
SqlCommand cmd = new SqlCommand(cmdText,cn);
string customerId = Customers.DataKeys[ e.Item.ItemIndex ].ToString();
cmd.Parameters.Add(new SqlParameter( "@CustomerID", customerId));
string companyName = ((TextBox)e.Item.Cells[ 1 ].Controls[ 0 ]).Text;
cmd.Parameters.Add(new SqlParameter( "@CompanyName" , companyName));
string contactName = ((TextBox)e.Item.Cells[ 2 ].Controls[ 0 ]).Text;
cmd.Parameters.Add(new SqlParameter( "@ContactName", contactName));
string contactTitle = ((TextBox)e.Item.Cells[ 3 ].Controls[ 0 ]).Text;
cmd.Parameters.Add(new SqlParameter( "@ContactTitle", contactTitle));
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
Customers.EditItemIndex = -1;
BindGrid();
}
private void BindGrid()
{
string connString = "user id=sa;password=sa;" +
"database=NorthWind;server=DeathAngel;";
SqlConnection cn = new SqlConnection(connString);
string cmdText = "Select * From Customers";
SqlCommand cmd = new SqlCommand(cmdText , cn);
cmd.Connection.Open();
Customers.DataSource = cmd.ExecuteReader();
Customers.DataBind();
cmd.Connection.Close();
}
}
}
page code:
VB Code:
<%@ Page language="c#" Codebehind="DataGridUpdate.aspx.cs"
AutoEventWireup="false" Inherits="CSharp.DataGridUpdate" %>
<html>
<body<>
<form runat="server" id="DataGridUpdate">
<asp:DataGrid
ID="Customers"
Runat="server"
AutoGenerateColumns="False"
OnEditCommand="Customers_EditCommand"
OnUpdateCommand="Customers_UpdateCommand"
OnCancelCommand="Customers_CancelCommand"
DataKeyField="CustomerID">
<Columns>
<asp:BoundColumn
ReadOnly="True"
HeaderText="Customer ID"
DataField="CustomerID"/>
<asp:BoundColumn
HeaderText="Company Name"
DataField="CompanyName"/>
<asp:BoundColumn
HeaderText="Contact Name"
DataField="ContactName"/>
<asp:BoundColumn
HeaderText="Contact Title"
DataField="ContactTitle"/>
<asp:EditCommandColumn
EditText="Edit"
UpdateText="Update"
CancelText="Cancel"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
-
Dec 20th, 2002, 01:51 AM
#3
Thread Starter
Frenzied Member
It works, tanks. But my general idea was to utilize oledb methods, like commandbuilder and dataadapter... and then I need to put the data in a dataset... I will try it myself, and when I succeed I will post the results here.
The good think with commandbuilder is that it makes the UPDATE query for you 
best regards
Henrik
-
Dec 20th, 2002, 05:41 AM
#4
Thread Starter
Frenzied Member
Oh, I had two questions also, perhaps you can answer them as well?
1) I am having problems with paging the datagrid when using the cmd.ExecuteReader() as datasource for the datagrid... can you make paging work?
2) A silly question, but if I want a label to adapt to dynamical controls (like the datagrid), how do I set that? Cause right now it has fixed position, and is put on top of the datagrid...
kind regards
Henrik
-
Dec 20th, 2002, 11:38 AM
#5
Hyperactive Member
If you're using MSSQL Server you'll want to use the SqlClient namespace, not OLEDB, SqlClient is optimized for SQL Server and has all of the DataAdapter and Commandbuilder stuff OLEDB has.
I don't like auto generated sql myself, so i tend to stay away from the commandbuilder.
For paging and sorting to work "easily" you'll want to use the dataset over the datareader. This guy's site - http://www.geocities.com/jeff_louie/NET_General.htm has some really good info and examples on how to accomplish that.
I don't understand your second question on the Label, maybe you could elaborate.
-
Dec 20th, 2002, 12:42 PM
#6
Thread Starter
Frenzied Member
Thanks for the reply
On the second question, first I place a datagrid on the "work area" then I place a webservercontrol label under that datagrid.
If the datagrid at runtime contains lots of info, it tend to get quite "long". When that happens, the label placed below the datagrid doesn't align itself under the datagrid, but it stays "on top" of the datagrid... It's position is fixed, no matter how much space the other dynamical controls take... I want the label to move if the datagrid "pushes" it downwards...
do you understand?
best regards
Henrik
-
Dec 20th, 2002, 01:32 PM
#7
Hyperactive Member
Well if you drag the label onto the designer(i'm not a huge fan of the designer for this reason) you're gonna get absolute positioning. Just need to modify the style attribute of that label control and remove the absolute position style.
-
Dec 20th, 2002, 06:53 PM
#8
PowerPoster
Right click on the web form and select properties. Then, change the layout property. I can't remember the exact selection, but there are only two (flow I believe) selections.....That should fix your absolute position problem.
-
Dec 27th, 2002, 02:24 AM
#9
Thread Starter
Frenzied Member
Hi, and sorry for my late reply. I couldn't find any properties that allowed me to change the positioning. So I solved it the good old fashioned way, with tables. Still working on the paging problem though.
best regards
Henrik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|