DATAGRID PROBLEM

S

Savas Ates

First When I Bind my datagrid It returns --Unisex-- value for my
GENDER_NAME field. When I click Edit , Write UNI to update UNÝSEX value and
Click Update button i catch

UPDATE TBL_GENDER SET GENDER_NAME='Unisex' WHERE GENDER_ID=1
with response.write ..

Why it doesnt return me current value of my Text1 texbox item. What is the
problem?




<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 190px; POSITION:
absolute; TOP: 195px"
runat="server" AutoGenerateColumns="False">

<Columns>

<asp:editcommandcolumn edittext="Edit" canceltext="Cancel"
updatetext="Update" itemstyle-wrap="false"

ItemStyle-Width="100px" />



<asp:TemplateColumn>



<EditItemTemplate >


<asp:TextBox ID="Text1" Runat=server Text='<%#
DataBinder.Eval(Container, "DataItem.GENDER_NAME") %>'>

</asp:TextBox>

<asp:TextBox ID="Label1" Runat=server Visible=False Text='<%#
DataBinder.Eval(Container, "DataItem.GENDER_ID") %>'>

</asp:TextBox>


</EditItemTemplate>


</asp:TemplateColumn>

</Columns>
</asp:DataGrid>



Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.EditCommand

Me.DataGrid1.EditItemIndex = e.Item.ItemIndex

DataGrid1.DataBind()





End Sub







Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.UpdateCommand





Dim sorgu As String

Dim strDestination As String = CType(e.Item.FindControl("Text1"),
TextBox).Text

Dim strID As String = CType(e.Item.FindControl("Label1"), TextBox).Text

Dim baglantim1 As New SqlClient.SqlConnection

sorgu = "UPDATE TBL_GENDER SET GENDER_NAME='" & strDestination & "' WHERE
GENDER_ID=" & strID

Response.Write(sorgu)

Response.End()

End Sub
 
C

Chris Chilvers

DataGrid1.DataBind()

This line looks suspect, calling databind will often end editing etc. It
will also reset the viewstate and postback handling on the datagrid.

Also double check that you don't call DataBind() every time you load the
page, only when you first load the page. eg:

If Not Page.IsPostBack() Then PopulateDatagrid()

Where PopulateDatagrid() is the method that will query the database and
call databind.


sorgu = "UPDATE TBL_GENDER SET GENDER_NAME='" & strDestination & "' WHERE
GENDER_ID=" & strID

As a side note this code is potentualy unsafe due to sql injection, see
http://www.codeproject.com/cs/database/SqlInjectionAttacks.asp for an
explaination and solution.
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top