Results 1 to 4 of 4

Thread: [RESOLVED] How can I use the same connection string on the same page?

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] How can I use the same connection string on the same page?

    I am currently unable to get a second new connection in the same page without errors unless I use the same connection code. I connect once to validate the user and then I connect once again via a method to get related data. I had to do this because I needed to cycle through a couple columns until there's no more related data. This is just how I had to do it to get it to work(newbie code). But anyhow, my question is, how can I use the same connection to the database to requery it without recreating it from scratch again? Is that possible?


    Here is the first connection:

    public void loadFamHist()
    {
    //Define database connection
    SqlConnection conn = new SqlConnection(
    "Server=localhost\\SqlExpress;Database=MyDB;" + "Integrated Security=True");
    //Create command
    SqlCommand comm = new SqlCommand("SELECT * FROM UsrTbl, RelativeTable, IP_Table WHERE RelativeTable.UserID IN (SELECT UserID FROM UsrTbl WHERE UserName = @usrnmeLbl)", conn);
    comm.Parameters.AddWithValue("@usrnmeLbl", usrNmeLbl.Text);
    //comm.Parameters.AddWithValue("@usrnmeLbl", crrntusr);
    conn.Open();
    SqlDataReader reader = comm.ExecuteReader();
    //Check to see if the reader has data(Did the username exist in the database? We must check this
    if (reader.Read() != false)
    {
    while (reader.Read())
    {
    string usr = reader["UserName"].ToString();



    then here is the second connection:

    private void getRelatives()
    {
    //Define database connection
    SqlConnection conn = new SqlConnection(
    "Server=localhost\\SqlExpress;Database=MyDB;" + "Integrated Security=True");
    //Create command
    SqlCommand comm = new SqlCommand("SELECT Relation, RelativeFN FROM RelativeTable WHERE RelativeTable.UserID IN (SELECT UserID FROM UsrTbl WHERE UserName = @usrnmeLbl)", conn);
    comm.Parameters.AddWithValue("@usrnmeLbl", usrNmeLbl.Text);
    conn.Open();
    SqlDataReader reader = comm.ExecuteReader();
    while (reader.Read())
    {
    Label4.Text += reader["Relation"].ToString() + ": ";
    Label4.Text += reader["RelativeFN"].ToString();
    Label4.Text += reader["RelativeLN"].ToString();


    Thanks in advance!

  2. #2
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: How can I use the same connection string on the same page?

    did you close the connection??

    If you didnt then you just reuse connection ie use your code from conn.open downwards

    Its not always a good idea to keep connections open

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: How can I use the same connection string on the same page?

    Thank you for this.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] How can I use the same connection string on the same page?

    Always close and destroy your connection once you're done with it. To comment on your code though, I would use a different connection object for each logical query I perform.

    Also, store your connection string in the web.config file, this way using the same connection object becomes an idiotic matter for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width