Showing posts with label snippet. Show all posts
Showing posts with label snippet. Show all posts

Saturday, February 25, 2012

ADODB to textBox

First time poster, I am using MS Access and I have used the following code to get some data. It is as follows:

Code Snippet

Private Sub FillGUI()
On Error Resume Next

Dim myRS2 As New ADODB.Recordset

myRS2.ActiveConnection = CurrentProject.Connection
myRS2.CursorType = adOpenDynamic
myRS2.LockType = adLockOptimisticd
myRS2.Open "SELECT E.SSN, E.LNAME, SUM(W.HOURS) FROM EMPLOYEE E, WORKS_ON W WHERE (E.SSN = W.ESSN)GROUP BY E.SSN, E.LNAME HAVING(SUM(HOURS)) < 40"

MsgBox (myRS2.GetString)
myRS2.MoveFirst

End Sub

and I have the following output:

http://www.angelfire.com/oh5/ohiostate120/untitled1.JPG

This is what I want. However I need this to be in a text box so I have the following code:

Code Snippet

Private Sub FillGUI()
On Error Resume Next

Dim myRS2 As New ADODB.Recordset

myRS2.ActiveConnection = CurrentProject.Connection
myRS2.CursorType = adOpenDynamic
myRS2.LockType = adLockOptimisticd
myRS2.Open "SELECT E.SSN, E.LNAME, SUM(W.HOURS) FROM EMPLOYEE E, WORKS_ON W WHERE (E.SSN = W.ESSN)GROUP BY E.SSN, E.LNAME HAVING(SUM(HOURS)) < 40"

Me.txtEmployee.SetFocus
Me.txtEmployee.Text = myRS2.GetString
myRS2.MoveFirst

End Sub

And i get this:

http://www.angelfire.com/oh5/ohiostate120/untitled2.JPG

How can I get the text box format to look like the msgbox format? Thanks.........

This is not an SSIS question.

You might try asking in the "Where is the forum for...?" forum to see if someone there can direct you to the correct place to ask your Access question.

Forum: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=881&SiteID=1

Thanks,
Phil Brammer|||Yeah I thought I put it in the wrong place. Thanks

Friday, February 24, 2012

ADO.net (how to close a reader)

Hello,
I'm trying to run the following snippet as Console app, and I get an error:
"a reader is already open for this connection, and should be closed first"
I tried to manually say:
SqlDataReader.Close() in the begining of the code, but still get the error, Any suggecstions how to manually close the reader?
thank you
---- here's the code ----
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace ADO.NET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

SqlConnection cn = new SqlConnection
("SERVER=MyServer; INTEGRATED SECURITY=TRUE;" +
"DATABASE=AdventureWorks");
SqlCommand cmd1 = new SqlCommand ("Select * from HumanResources.Department", cn);
cmd1.CommandType = CommandType.Text;
try
{
cn.Open();
SqlDataReader rdr = cmd1.ExecuteReader();
while (rdr.Read())
{
if (rdr["Name"].ToString() == "Production")
{
SqlCommand cmd2 = new SqlCommand("SELECT * FROM "
+ "HumanResources.Employee WHERE DepartmentID = 7", cn);
cmd2.CommandType = CommandType.Text;
SqlDataReader rdr2 = cmd2.ExecuteReader();
while (rdr2.Read())
{
}
rdr2.Close();
}
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine
(ex.Message);
}
finally
{
cn.Close();
}
}
}
}

Only one reader at a time can be active on a single connection. Create a second connection and all will be well.

This is not a real problem, since connection pooling will make efficient use of connections.

|||thanks!