Hi,
I've an issue in my datagrid Update Event of datagrid is called when i refresh the page the record is saved more then one time in database. How should i do to save record only one time not refresh the page? Plz help me.
Thanks,
Hi,
I've an issue in my datagrid Update Event of datagrid is called when i refresh the page the record is saved more then one time in database. How should i do to save record only one time not refresh the page? Plz help me.
Thanks,
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
HinaPosted Apr 3, 2008, 8:34 AM
No, its a "Make a Copy" button in grid when i click it getes the same latter_ID from selected letter which i want to copy and insert this on database with new ID and then refresh the page its insert again same process get selected letter id and insert it in the database.
Thanks,
Hina
Niradhip ChakrabortyPosted Apr 3, 2008, 8:29 AM
R u using store procedure???
HinaPosted Apr 3, 2008, 8:06 AM
yup the problem is that when Insert the first time and then refresh the page datagrid Update Cammand Event Fired automatically and record is inserted. What should i do for this plz urgently told me.
Thanks,
Niradhip ChakrabortyPosted Apr 3, 2008, 8:03 AM
HinaPosted Apr 3, 2008, 7:49 AM
hi,
thanks for reply, ok i am told you complete scenarion. I've datagrid with some bounds column and ButtonColumn. One buttoncolumn has command="Update" On Update Cammand event Insert the record in database. When Insert first time and press F5 or Refresh the page this event is occure and record is inserted. What should i do? Plz Help me.
Regards,
Hina
Niradhip ChakrabortyPosted Apr 3, 2008, 7:29 AM
The data is saved more than one time because while updating you are not specifing
which row to update. Hence it is updating all the row in the datagrid.
Use datakey to specify the row
like dgrdDayEnd.DataKeys(CInt(dgrdItem.ItemIndex))
dgrdDayEnd is my datagrid name
and dgrdItem is DataGridItem
or u can follow the following code:
HTML part of the code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DGInsert.aspx.vb" Inherits="DotNetJohn.DGInsert"%>
http://schemas.microsoft.com/intellisense/ie5">
Shipper Code Maintenance
//code behind for the page
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class DGInsert
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim da As SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub
Sub BindGrid()
Try
sqlConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim strSQL As String = "SELECT ShipperID, CompanyName FROM Shippers ORDER BY CompanyName"
Dim da As New SqlDataAdapter(strSQL, sqlConn)
Dim ds As New DataSet()
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
Catch ex As SqlException
Response.Write(ex.ToString())
End Try
End Sub
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
If e.CommandName = "Insert" Then
Dim tb1 As TextBox = e.Item.FindControl("txtCompanyName")
If tb1.Text.Trim() <> "" Then
Dim strSQL As String
Try
sqlConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
strSQL = "INSERT INTO Shippers(CompanyName) values('" & tb1.Text.Trim() & "')"
sqlConn.Open()
sqlCmd = New SqlCommand(strSql, sqlConn)
sqlCmd.ExecuteNonQuery()
BindGrid()
Catch ex As SqlException
Response.Write(ex.ToString())
End try
End If
End If
End Sub
Sub ChangePage(sender As Object, e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
End Class