views:

2677

answers:

3

I'm creating a dynamic gridview function which will bind different tables from DB into a datatable and then assign the datatable to the gridview! This is how it works, i have a dropdownlist, gridview and a button, the button will fire specific function based on the dropdownlist selection and then gridview will bind the data, my problem is that, when u press the button for the 1st time, the gridview will bind the data from DB, for second time pressing, the gridview will duplicate the data from the data of 1st time pressing! how do i clear off the gridview to avoid data duplication?

Private Sub Login()
    sSql = "" & _
    "SELECT TYPE, SUBTYPE, LOGTS, ACTION, USERID, STAT1 " & _
    "FROM i_LOG " & _
    "WHERE TYPE = 'USR' AND SUBTYPE = 'LOG' " & _
    "AND CONVERT(VARCHAR(20), LOGTS, 103) >= '" & txtDTFrom.Text & _
    "' AND CONVERT(VARCHAR(20), LOGTS, 103) <= '" & txtDTTo.Text & "'"

    DT = CreateDataTable(sSql)     'Retrieve from database.

    Session(sSesDT) = DT
    GVM.DataTable = DT
    GVM.GVAddEmptyRow()

    Dim seq As New BoundField
    Dim Type As New BoundField
    Dim SubType As New BoundField
    Dim Logts As New BoundField
    Dim action As New BoundField
    Dim user As New BoundField
    Dim status As New BoundField

    seq.HeaderText = GVM.DTNumberField
    seq.DataField = GVM.DTNumberField

    Type.HeaderText = "Type"
    Type.DataField = "TYPE"

    SubType.HeaderText = "Subtype"
    SubType.DataField = "SUBTYPE"

    Logts.HeaderText = "Date and Time"
    Logts.DataField = "LOGTS"

    action.HeaderText = "Action"
    action.DataField = "ACTION"

    user.HeaderText = "User ID"
    user.DataField = "USERID"

    status.HeaderText = "Status"
    status.DataField = "STAT1"

    gv.AutoGenerateColumns = False
    gv.Columns.Add(Type)
    gv.Columns.Add(SubType)
    gv.Columns.Add(Logts)
    gv.Columns.Add(action)
    gv.Columns.Add(user)
    gv.Columns.Add(seq)
    gv.Columns.Add(status)

    gv.DataSource = Session(sSesDT)
    gv.DataBind()
End Sub

Private Overloads Function CreateDataTable(ByVal sSql As String) As DataTable
    Dim DA As New OleDb.OleDbDataAdapter
    Dim dtDataTbl As New DataTable
    Dim dcDataCol As New DataColumn

    With DBMgr
        .openCnn()
        .SQL = sSql
        .openRst()
        DA.Fill(dtDataTbl, .rst)
    End With

    '==Adding Columns==
    dcDataCol = New DataColumn  'No
    With dcDataCol
        .DataType = GetType(Int32)
        .ColumnName = GVM.DTNumberField
        .AutoIncrement = True
        '.AllowDBNull = True
    End With
    dtDataTbl.Columns.Add(dcDataCol)
    '**Adding Columns**

    Return dtDataTbl
End Function
+1  A: 

Sorry, this is in C#, but just call it before you bind any new data and it'll clear any existing data... unless of course the problem is that the DataSource you are assigning has the duplicated data.

gridview.DataSource=null;
gridview.DataBind();

Also, just to note, you are adding every column in every time that procedure is called? You need to run:

gridview.Columns.Clear();
GenericTypeTea
thanks! it works!
WeeShian
A: 

You can reset it's data source and rebind it for example

gv.datasource=""
gv.databind()

and then reuse it

v3ga
you can not assign empty string to gridview datasorce,please try it out .
Ramakrishnan
A: 

Usually this does not happen when you rebind grid like:

grid.DataSource = 'NEW_DATA_SOURCE'
grid.DataBind

I think may be the problem is in your function fetching data:

 CreateDataTable(sSql)

Please check inside this if you are fetching data every time into a new DataTable or using same one (may be declared at global scope or passed from your class to data accessing component). I'd lost whole night once to fix this :(

TheVillageIdiot