update a row in data grid

G

Guest

actually my code looks like this

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.Columns[5].Visible=true;
DataGrid1.Columns[6].Visible=true;
TextBox t1=new TextBox();
TextBox t6=new TextBox();
TextBox t7=new TextBox();

t1.Text=((TextBox)e.Item.FindControl("TextBox10")).Text;
t2.Text=((TextBox)e.Item.Cells[2].FindControl("TextBox8")).Text;
t3.Text=((TextBox)e.Item.Cells[3].FindControl("TextBox9")).Text;



SqlConnectioncon=DataAccessor.CreateConnection();
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandType=CommandType.Text;


cmd.CommandText=@"update tblPQOrder set
ProductID="+t1.Text+","+"Quantity="+t2.Text+","+"UnitCost="+t3.Text+"where
ItemID="+DataGrid1.DataKeys[e.Item.ItemIndex];





cmd.ExecuteReader();
DataAccessor.CloseConnection(con);
cmd.Dispose();
Rebind();

}

But when i am updating a new item thw value is not coming to the code...,Is
there any mistake in the SQL update statment??
 
M

Michelle Hlaing

Hi Jijo,

This is how I would write my update command:

SqlConnection cn = new SqlConnection();
string strCn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
string cmd = "Update tblUsers set Name = "+name+", Title = "+title+", SysAdmin = '"+sSA+"', Operations = '"+sOP+"', SupportLimit = "+slimit+",
ConsultingLimit = "+climit+" where Alias = '"+alias+"'";
cn.ConnectionString = strCn;
myCmd = new SqlCommand(cmd, cn);
myCmd.Connection.Open();
myCmd.ExecuteNonQuery();

where Boolean values like SysAdmin, Operations have single quotes around them. (it depends on your value type)

You can choose to do it a few different ways from here on. One of them is to

1) Fill a dataset with the latest information from the DB
2) Datagrid.DataSource = dataset
3) Datagrid.DataBind(); should display the changes.

Hope that helps,

Michelle Hlaing

Microsoft Support Professional

***Disclaimer: This posting is provided "as is" with no warranties and confers no rights.***
--------------------
Thread-Topic: update a row in data grid
thread-index: AcTQvhDzHmaOdu9mTPipQTskyxZklg==
X-WBNR-Posting-Host: 202.88.237.151
From: "=?Utf-8?B?amlqbyBrdXJ1dmlsYQ==?=" <[email protected]>
Subject: update a row in data grid
Date: Mon, 22 Nov 2004 10:07:02 -0800
Lines: 43
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:288356
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

actually my code looks like this

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.Columns[5].Visible=true;
DataGrid1.Columns[6].Visible=true;
TextBox t1=new TextBox();
TextBox t6=new TextBox();
TextBox t7=new TextBox();

t1.Text=((TextBox)e.Item.FindControl("TextBox10")).Text;
t2.Text=((TextBox)e.Item.Cells[2].FindControl("TextBox8")).Text;
t3.Text=((TextBox)e.Item.Cells[3].FindControl("TextBox9")).Text;



SqlConnectioncon=DataAccessor.CreateConnection();
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandType=CommandType.Text;


cmd.CommandText=@"update tblPQOrder set
ProductID="+t1.Text+","+"Quantity="+t2.Text+","+"UnitCost="+t3.Text+"where
ItemID="+DataGrid1.DataKeys[e.Item.ItemIndex];





cmd.ExecuteReader();
DataAccessor.CloseConnection(con);
cmd.Dispose();
Rebind();

}

But when i am updating a new item thw value is not coming to the code...,Is
there any mistake in the SQL update statment??
 

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