0
Answer

Updating DataGridView within Update Panel

Chris Clarke

Chris Clarke

15y
6.3k
1
Hello, I'm hoping someone can help me, I've been going around in circles for days now and can't find the solution anywhere. Upon Page Load I generate a session ID that is stored within a hidden control (label) on the form. I have two text boxes (txtSerial and txtReturn) and a button (cmdAddSerial). When this is clicked my code goes off and inserts a row into an SQL db. On the same form I have a datagridview (dgSerials) this is linked to a datasource (dsSerials). The idea is that as users enter serial numbers, the datagridview updates at the bottom showing them what they have done so far. Firstly I didn't realise you could only update the whole page or nothing. Since then I have read I need to use an update panel for my datagridview. I have also put my cmdAddSerials button within the panel. Unfortunately, it just will not update the view - I've tried coding conditional calls, letting it update always with every child control and setting up triggers - none of it works, and its driving me nuts for something that should be so simple!! Heres my code, note that unless I put this line in: If Not Page.IsPostBack Then - generate my session id, the page always reloads and generates a new session id which means my inserted records go in against a unique session and only display one row at a time. Heres the Page Code, any light anyone can shed would be greatly appeciated. TIA. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Call GenerateSession() End Sub Protected Sub GenerateSession() If Not Page.IsPostBack Then lblCustomerID.Text = mdlConstants.getCurrentCompany Dim strQry As String Dim CurrentSessionID As Integer Dim NewSessionID As Integer strQry = "SELECT Return_Session_ID FROM Session_ID" Dim connStr As String = ConfigurationManager.ConnectionStrings("FUSION_AWSConnectionString").ConnectionString 'Open the connection and execute the query Dim Conn As New SqlClient.SqlConnection(connStr) Dim Comm As New SqlClient.SqlCommand(strQry, Conn) Dim Dr As SqlClient.SqlDataReader Conn.Open() Dr = Comm.ExecuteReader() Dr.Read() CurrentSessionID = Dr("Return_Session_ID") lblSessionID.Text = CurrentSessionID Conn.Close() Conn = Nothing Comm = Nothing Dr = Nothing NewSessionID = CurrentSessionID + 1 Dim strUpd As String strUpd = "UPDATE Session_ID SET Return_Session_ID = '" & NewSessionID & "'" Dim ConnUpd As New SqlClient.SqlConnection(connStr) Dim CommUpd As New SqlClient.SqlCommand(strUpd, ConnUpd) ConnUpd.Open() CommUpd.ExecuteNonQuery() ConnUpd.Close() ConnUpd = Nothing CommUpd = Nothing End If End Sub Protected Sub cmdAddSerial_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdAddSerial.Click Dim strQry As String strQry = "INSERT INTO CRN_Serial_Temp (Session_ID, Serial_Number, Return_Reason) VALUES(@Session, @Serial, @Reason); SELECT SCOPE_IDENTITY()" Dim connStr As String = ConfigurationManager.ConnectionStrings("FUSION_AWSConnectionString").ConnectionString Dim Conn As New SqlClient.SqlConnection(connStr) Dim Comm As New SqlClient.SqlCommand(strQry, Conn) Comm.Parameters.AddWithValue("@Session", lblSessionID.Text) Comm.Parameters.AddWithValue("@Serial", txtSerial.Text) Comm.Parameters.AddWithValue("@Reason", txtReason.Text) Conn.Open() Comm.ExecuteNonQuery() Conn.Close() If Page.IsPostBack Then txtSerial.Text = "" txtReason.Text = "" End If End Sub