Click here to Skip to main content
11,415,164 members (74,295 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
public string user(AddInfoModel info)
        {
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
             con.Open();
             SqlCommand cmd = new SqlCommand("Resource_sp", con);
             cmd.CommandType = CommandType.StoredProcedure;
             SqlDataReader reader = cmd.ExecuteReader();
             if (reader.Read())
                {
 
                    FirstName.Text = reader["FirstName"].ToString();
                    LastName.Text = reader["LastName"].ToString();
                    reader.Close();
 
                    con.Close();
                }
            }
        }
    }
Posted 18-Jan-13 23:21pm
Edited 18-Jan-13 23:34pm
Abhinav S394.3K
v2
Comments
rizwan muhammed khan gouri at 19-Jan-13 4:35am
   
what is your problem or error.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you are getting multiple rows, then you need to note that you are rewriting into the textbox. This is not correct as only the values in the last row would be displayed. Use a Datagrid or a Listbox instead of a textbox.
  Permalink  
v2
Comments
Angelina Feb21 at 19-Jan-13 4:47am
   
i need only the one record
Rachna0309 at 19-Jan-13 5:10am
   
Your code is correct..What is the error??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use reader.close() and con.close() outside the if condition

public string user(AddInfoModel info)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Resource_sp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{

FirstName.Text = reader["FirstName"].ToString();
LastName.Text = reader["LastName"].ToString();

}
reader.Close();
con.Close();

}
}
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,200
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.4 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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