I have a Function which returns DataTable Like this (if there are Rows)...
Protected Function getAideData() As DataTable
Dim dt As DataTable = DAL.ReturnData("select * from pg_PersonalInfo P Left Join pg_employeeInterview E on E.sesID = P.sesID ")
If dt.Rows.Count > 0 Then
Return dt
End If
End Function
Then later in my page I accress it like this:
Dim d as datatable = getAideData
Here is my problem: If I have Data in Datatable then I have no problem, however if I dont have any data returned from my method then there is a issue.
I guess my question is, if I have a Function and that cannot return a datatable (no rows), then what should I return from my function? So i can handle the data properly later in my application.
I guess my question is, if I have a Function and that cannot return a
datatable (no rows), then what should I return from my function?
Return Nothing
Protected Function getAideData() As DataTable
Dim dt As DataTable = DAL.ReturnData("select * from pg_PersonalInfo P Left Join pg_employeeInterview E on E.sesID = P.sesID ")
If dt.Rows.Count > 0 Then
Return dt
End If
Return Nothing
End Function
Function GetDataTable(ByVal qry As String) As DataTable
Dim DbCon As New OleDb.OleDbConnection
Try
Dim ConStr As String
ConStr = System.Configuration.ConfigurationManager.AppSettings("ConnCMSTrend").ToString()
DbCon.ConnectionString = ConStr
DbCon.Open()
Dim queryString As String = qry
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(queryString, ConStr)
Dim res As DataSet = New DataSet
adapter.Fill(res)
DbCon.Close()
GetCMSTrend = res.Tables(0)
Catch ex As Exception
DbCon.Close()
End Try
End Function
'Note That you need to add the connection string in the appsettings file. In the above case ConnCMSTrend is the database connection string.
Related
I'm having a problem with an ASP 2005 application, the problem is that I´m calling a stored procedure in Oracle saved on a package, that procedure is very heavy (has like 1000 lines) and at the end it inserts into a table the data that it just calcutaded, then in other vb function I get the information from that table and show it on a gridview, but the 2 columns that are calculated sometimes come with value 0 and sometimes come with the values that should be. This only happens when I call the precedure from asp, when I run it from Toad it always get values on those columns... Do you have an idea of what could be happening??
This is the function I use for sending parameters:
Private Function LoadTable(ByVal tipee As String, ByVal period As String, ByRef ErrorUser As String, ByVal dateInit As String, ByVal dateEnd As String) As Boolean
Dim cmd As New OracleCommand
cmd.CommandText = "Schema.PKG.procedureA"
cmd.CommandType = CommandType.StoredProcedure
Dim Prmts(6) As OracleClient.OracleParameter
Prmts(0) = New System.Data.OracleClient.OracleParameter("ptipotrans", OracleType.VarChar)
Prmts(0).Value = tipee
Prmts(1) = New System.Data.OracleClient.OracleParameter("pperiodo", OracleType.VarChar)
Prmts(1).Value = period
Prmts(2) = New System.Data.OracleClient.OracleParameter("pfechaini", OracleType.VarChar)
If dateInit = "" Then
Prmts(2).Value = DBNull.Value
Else
Prmts(2).Value = dateInit
End If
Prmts(3) = New System.Data.OracleClient.OracleParameter("pfechafin", OracleType.VarChar)
If dateEnd = "" Then
Prmts(3).Value = DBNull.Value
Else
Prmts(3).Value = dateEnd
End If
Prmts(4) = New System.Data.OracleClient.OracleParameter("perror", OracleType.VarChar, 100)
Prmts(4).Direction = ParameterDirection.Output
cmd.Parameters.Clear()
For i As Integer = 0 To 4
cmd.Parameters.Add(Prmts(i))
Next
Dim cls_sql As New cls_ejecutaSql
Return cls_sql.exec_ens(cmd, errorUser)
End Function
And this the function I use for connecting to Oracle:
Public Function exec_ens(ByVal cmd As OracleCommand, ByRef msg As String) As Boolean
Dim lint_resul As Boolean
con.Open()
cmd.Connection = con
Try
cmd.ExecuteNonQuery()
lint_resul = True
Catch ex As Exception
msg = cmd.Parameters("perror").Value
lint_resul = False
Finally
con.Close()
End Try
Return lint_resul
End Function
I don't see anything out of the normal in those functions, and I also verified that the parameters are being sent well
I have a problem when setting the selected value of a dropdownlist inside a gridview with the data from a datatable.
I have an SQL query that selects the data that must be displayed from the dropdownlist. The dropdownlist was already populated. Therefore, the result of the query must be be displayed in the dropdownlist.
I tried below the code, but it returns the same value on each row.
This one is the code for selecting the data from database:
Public Function POPULATE_DROPDOWNLIST(ByVal d_Date As Date, ByVal d_Hour As Integer)
Using con As Odbc.OdbcConnection = New Odbc.OdbcConnection(My.Settings.DB_CONN)
Using cmd As Odbc.OdbcCommand = _
New Odbc.OdbcCommand( _
"SELECT * FROM TBLDATA WHERE D_DATE='" & d_Date & _
"' AND DELIVERY_HOUR='" & d_Hour & "'",
con)
con.Open()
Using dr As Odbc.OdbcDataReader = cmd.ExecuteReader
Dim d_List As New List(Of String)
While dr.Read
d_List.Add(dr("CRITERIA").ToString())
End While
con.Close()
Return d_List
End Using
End Using
End Using
End Function
While this one is my code for populating gridview dropdownlist with the data returned by above function.
Dim dList As New List(Of String)
dList = POPULATE_DROPDOWNLIST(txtdate.Text, DropDownList1.Text)
For Each value In dList
For i = 0 To GridView1.Rows.Count - 1
Dim ddl As DropDownList = _
DirectCast(GridView1.Rows(i).Cells(6).FindControl("dropdowncriteria"), _
DropDownList)
ddl.SelectedValue = value
Next
Next
Try this:
Public Function POPULATE_DROPDOWNLIST(ByVal d_Date As Date, ByVal d_Hour As Integer) As DataTable
Dim dataTable As New DataTable
Using con As New Odbc.OdbcConnection("CS")
Using cmd As New Odbc.OdbcCommand
cmd.CommandText = "QUERY"
con.Open()
Dim dataAdapter As New Odbc.OdbcDataAdapter(cmd.CommandText, con)
dataAdapter.Fill(dataTable)
Return dataTable
End Using
End Using
End Function
Public Sub BIND_DDL()
Dim previousDDLvalue As String = ddl.SelectedValue
Dim dt As New DataTable
dt = POPULATE_DROPDOWNLIST(Now, 24)
With ddl
.Items.Clear()
.DataSource = dt
.DataValueField = "ID"
.DataTextField = "DESCRIPTION"
.DataBind()
.SelectedValue = previousDDLvalue
End With
End Sub
I need to databind the gridview in which the string come from other system.
The format will like that:
A1|Peter|2011-01-01|2012-03-03|0;A9|Jacky|2011-10-01|2012-09-03|2;B3|Chris|2011-10-01|2012-09-03|2;
| as separator of column
; as separator of next record
Is there any method to do that ?
Thanks.
my code as below:
Sub Call_Payroll(ByVal SNO As String)
Dim theType As String = ""
theType = xxxxxx <- Get from Source
Dim tList = theType.Split(";")
Dim dt As New DataTable
dt = ListToDataTable(tList)
RadGrid_payroll.DataSourceID = ""
RadGrid_payroll.DataSource = dt
RadGrid_payroll.DataBind()
End Sub
Public Shared Function ListToDataTable(Of T)(ByVal list As List(Of T)) As DataTable
Dim dt As New DataTable()
For Each info As PropertyInfo In GetType(T).GetProperties()
dt.Columns.Add(New DataColumn(info.Name, info.PropertyType))
Next
For Each T In list
Dim row As DataRow = dt.NewRow()
For Each info As PropertyInfo In GetType(T).GetProperties()
row(info.Name) = info.GetValue(T, Nothing)
Next
dt.Rows.Add(row)
Next
Return dt
End Function
Is it possible to populate a listview with a dataset? I have a function that returns a dataset. Why im asking this is because my SQL is quite complicated and i can't convert it to a SQLDataSource...
Public Function getMessages() As DataSet
Dim dSet As DataSet = New DataSet
Dim da As SqlDataAdapter
Dim cmd As SqlCommand
Dim SQL As StringBuilder
Dim connStr As StringBuilder = New StringBuilder("")
connStr.AppendFormat("server={0};", ConfigurationSettings.AppSettings("USERserver").ToString())
connStr.AppendFormat("database={0};", ConfigurationSettings.AppSettings("USERdb").ToString())
connStr.AppendFormat("uid={0};", ConfigurationSettings.AppSettings("USERuid").ToString())
connStr.AppendFormat("pwd={0};", ConfigurationSettings.AppSettings("USERpwd").ToString())
Dim conn As SqlConnection = New SqlConnection(connStr.ToString())
Try
SQL = New StringBuilder
cmd = New SqlCommand
SQL.Append("SELECT m.MESSAGE_ID, m.SYSTEM_ID, m.DATE_CREATED, m.EXPIRE_DATE, ISNULL(s.SYSTEM_DESC,'ALL SYSTEMS') AS SYSTEM_DESC, m.MESSAGE ")
SQL.Append("FROM MESSAGE m ")
SQL.Append("LEFT OUTER JOIN [SYSTEM] s ")
SQL.Append("ON m.SYSTEM_ID = s.SYSTEM_ID ")
SQL.AppendFormat("WHERE m.SYSTEM_ID IN ({0}) ", sSystems)
SQL.Append("OR m.SYSTEM_ID is NULL ")
SQL.Append("ORDER BY m.DATE_CREATED DESC; ")
SQL.Append("SELECT mm.MESSAGE_ID, mm.MODEL_ID, m.MODEL_DESC ")
SQL.Append("FROM MESSAGE_MODEL mm ")
SQL.Append("JOIN MODEL m ")
SQL.Append(" ON m.MODEL_ID = mm.MODEL_ID ")
cmd.CommandText = SQL.ToString
cmd.Connection = conn
da = New SqlDataAdapter(cmd)
da.Fill(dSet)
dSet.Tables(0).TableName = "BASE"
dSet.Tables(1).TableName = "MODEL"
Return dSet
Catch ev As Exception
cLog.EventLog.logError(ev, cmd)
Finally
'conn.Close()
End Try
End Function
Where to start with this one...
listview.DataSource = getMessages().Tables(0);
listview.DataBind;
Sure. In the code-behind just set the datasource to the DataTable you want to bind the list to, then call DataBind() on the list:
Protected Sub Page_Load(ByVal sender As object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Me.LoadData()
End If
End Sub
Protected Sub LoadData()
Dim ds As DataSet = Me.GetData()
Me.lstSample.DataSource = ds("Model")
Me.lstSample.DataBind()
End Sub
(hopefully that's valid VB.NET code, I normally code in C#).
I a function that used a mySQL Data Adapter and inserts a new row.
I want the function to return the value of the auto-incremented column of the updated row.
Please help.
Public Function addRow(ByVal colA as String, ByVal colB as String) As Integer
Dim dbConnection As New MySqlConnection("server=xxx; user id=xxx; password=xxx; database=xxx; pooling=false;")
Dim dbDataAdapter As New MySqlDataAdapter("SELECT * FROM table", dbConnection)
Dim dbDataSet As DataSet = New DataSet
dbDataAdapter.Fill(dbDataSet, "table")
Dim dbNewRow As DataRow = dbDataSet.Tables("table").NewRow
dbNewRow("colA") = colA
dbNewRow("colB") = colB
dbDataSet.Tables("table").Rows.Add(dbNewRow)
dbDataAdapter.InsertCommand = New MySqlCommand("INSERT into table(colA, colB) VALUES (#colA, #colB)", dbConnection)
dbDataAdapter.InsertCommand.Parameters.Add("colA", MySqlDbType.VarChar, 256, "colA")
dbDataAdapter.InsertCommand.Parameters.Add("colB", MySqlDbType.VarChar, 256, "colB")
Dim dbParm As MySqlParameter = dbDataAdapter.InsertCommand.Parameters.Add("#colID", MySqlDbType.Int16, 16, "colID")
dbParm.Direction = ParameterDirection.ReturnValue
dbDataAdapter.Update(dbDataSet, "table")
Return dbDataAdapter.InsertCommand.Parameters("#colID").Value
End Function
The "Return..." line doesn't work and don't know what the correct code would be.
Assuming the insert stored procedure is doing something like
RETURN ##IDENTITY
You will need the equivalent of this:
oDatabase.AddParameter(oDbCommand, "#RETURN_VALUE", System.Data.DbType.Int32, System.Data.ParameterDirection.ReturnValue, "", System.Data.DataRowVersion.Current, null);
...
oDbCommand.ExecuteNonQuery();
returnStatusCode = (int)oDbCommand.Parameters[0].Value;
Try this:
Dim result As Integer = 0
Integer.TryParse(dbDataAdapter.InsertCommand.Parameters("#colID").Value, result)
Return result
I did it myself again.
Public Function addRow(ByVal colA as String, ByVal colB as String) As Integer
Dim dbConnection As New MySqlConnection("server=xxx; user id=xxx; password=xxx; database=xxx; pooling=false;")
Dim dbDataAdapter As New MySqlDataAdapter("SELECT * FROM table where colID = 0", dbConnection)
Dim dbDataSet As DataSet = New DataSet
dbDataAdapter.Fill(dbDataSet, "table")
Dim dbNewRow As DataRow = dbDataSet.Tables("table").NewRow
dbNewRow("colA") = colA
dbNewRow("colB") = colB
dbDataSet.Tables("table").Rows.Add(dbNewRow)
dbDataAdapter.InsertCommand = New MySqlCommand("INSERT into table(colA, colB) VALUES (#colA, #colB); Select * from table where colID = LAST_INSERT_ID()", dbConnection)
dbDataAdapter.InsertCommand.Parameters.Add("colA", MySqlDbType.VarChar, 256, "colA")
dbDataAdapter.InsertCommand.Parameters.Add("colB", MySqlDbType.VarChar, 256, "colB")
dbDataAdapter.Update(dbDataSet, "table")
Return dbDataSet.Tables("table").Rows(0).Item(0)
End Function
Seems all I needed to do was add a Select statement on the end of the Insert statement and use the LAST_INSERT_ID function to just get the last Inserted row. Then just read from that one rows ID and return it.
I am quite disappointed that DataAdapters work this way. I'm sure classic ASP was easier than this! Thanks to all who responded.