Click here to Skip to main content
14,667,966 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Both the GetTotalRows method and the GetTrackData method use the same SqlConnection object. I'm sure one of them should use myConnection1 instead.
   
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).
Rate this:
Please Sign up or sign in to vote.

Solution 3

This is pretty much an "is it plugged in" type of answer, but I think it needs to be asked.

When you call
public void GetTrackData(int TrackId)

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100