Click here to Skip to main content
12,509,932 members (57,897 online)
Rate this:
 
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 22:21pm
Updated 18-Jan-13 22:34pm
Abhinav S413.6K
v2
Comments
rizwan muhammed khan gouri 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 19-Jan-13 4:47am
   
i need only the one record
Rachna0309 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160929.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2016
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