Below is my code which is throwing Exception: Public Function populateDataset(ByVal strQuery As String, ByRef ds As DataSet, Optional ByVal strTableName As String = "") As Int16 Try If Not IsNothing(ds.Tables(strTableName)) Then ds.Tables(strTableName).Clear() End If Catch ex As Exception End Try populateDataset = -1 checkConnection() Try oledbDA = New MySqlDataAdapter(strQuery, cnDB) If strTableName = "" Then oledbDA.Fill(dataSet:=ds) Else oledbDA.Fill(dataSet:=ds, srcTable:=strTableName) End If populateDataset = 0 oledbDA.Dispose() Catch ex As MySqlException Throw ex Return -1 Catch ex As Exception populateDataset = -1 Throw ex End Try End Function Here i am calling it: public void getItemCategory(ref DataSet ds,string qry,string tableName) { dlib.CloseConnection(); dlib.populateDataset(qry, ref ds, tableName); }