Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys... im trying to write a class library inwhich I handle two different connections to two different DBs. Here is some of the library code...
SqlConnection myConnection1,myConnection2;
public bool OpenAdminConnection()
{
    myConnection1 = new SqlConnection("Data Source=muze-pc\\SqlExpress; Initial Catalog=adminDB; Integrated Security=SSPI");
    try
    {
        myConnection1.Open();
        SetData(); //a function used, working properly
        return true;
    }
    catch(Exception e)
    {
        myConnection1.Close();
        return false;
    }
}
/////////////////////Second connection to 2nd DB
public void OpenTracksConnection()
{
    myConnection2 = new SqlConnection("Data Source=muze-pc\\SqlExpress; Initial Catalog=MusicDB; Integrated Security=SSPI; MultipleActiveResultSets=True");
    try
    {
        myConnection2.Open();
    }
    catch (Exception e)
    {
        myConnection2.Close();
    }
}

public int GetTotalRows()//returns number of (already distinct) records
{
    SqlCommand myComm = new SqlCommand("SELECT COUNT(*) FROM Tracks", myConnection2);
    int totalRows = 0;
    SqlDataReader dataReader;
    dataReader = myComm.ExecuteReader();
    while (dataReader.Read())
    { totalRows++; }
    dataReader.Close();
    return totalRows;
}

public void GetTrackData(int TrackId) //returns info about track using key
{
    SqlCommand myComm = new SqlCommand("SELECT * FROM Tracks WHERE TrackID = " + TrackId.ToString(), myConnection2);
    SqlDataReader dataReader;
    dataReader = myComm.ExecuteReader();
    dataReader.Read();
    td.SetTrackData(dataReader[0].ToString(),dataReader[1].ToString(),dataReader[2].ToString());
    dataReader.Close();
}

Now connection1 is owrking properly but when I call GetTrackData(trackId) in my client, it says that no data is present in the dataReader. What can be the reason? thnx
Posted

public int GetTotalRows()//returns number of (already distinct) records
{
    SqlCommand myComm = new SqlCommand("SELECT COUNT(*) FROM Tracks", myConnection2);    
    int totalRows = 0;    
    SqlDataReader dataReader;    
    dataReader = myComm.ExecuteReader();    
    while (dataReader.Read())
    { 
      totalRows++; 
    }    
    dataReader.Close();
    return totalRows;
}


Returns 1.

Use totalRows = Convert.ToInt32( SqlCommand.ExecuteScalar() );
The query returns the number of rows.

GetTrackData:
if(dataReader.Read()) // Check that you actually got a result
{
  // I'd rather use the GetXXX methods
  td.SetTrackData(dataReader.GetString(0),
         dataReader.GetString(1),
         dataReader.GetString(2));
}



Regards
Espen Harlinn
 
Share this answer
 
v2
Both the GetTotalRows method and the GetTrackData method use the same SqlConnection object. I'm sure one of them should use myConnection1 instead.
 
Share this answer
 
Comments
AmbiguousName 19-Jan-11 8:26am    
nope...they use same connection i-e myConnection2, because I need number of rows and row data from database2 (not from DB1).
This is pretty much an "is it plugged in" type of answer, but I think it needs to be asked.

When you call
C#
public void GetTrackData(int TrackId)

while testing have you ensured that a record in the Tracks table actually exists with that value?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900