简体   繁体   中英

c# populate textboxes using sqldatareader method

I created the below method which i tested and does return the correct data. Where I am confused is what is the proper way to populate individual textboxes on a form with the results from this method?

Rather than using an objectdatasource and then binding a gridview to the objectdatasource that works but I need more freedom to customize the form.

public MemberDetails GetMemberDetail(int membershipgen)
   {
       SqlConnection con = new SqlConnection(connectionString);
       SqlCommand cmd = new SqlCommand("usp_getmemberdetail", con);
       cmd.CommandType = CommandType.StoredProcedure;

       cmd.Parameters.Add(new SqlParameter("@MEMBERSHIPGEN", SqlDbType.Int, 5));
       cmd.Parameters["@MEMBERSHIPGEN"].Value = membershipgen;

       try
       {
           con.Open();
           SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

           reader.Read();
           MemberDetails mem = new MemberDetails((int)reader["MEMBERSHIPGEN"], (string)reader["MEMBERSHIPID"], (string)reader["LASTNAME"],
                    (string)reader["FIRSTNAME"], (string)reader["SUFFIX"], (string)reader["MEMBERTYPESCODE"]);
           reader.Close();
           return mem;
       }
        catch (SqlException err)
       {
            throw new ApplicationException("Data error.");
        }
        finally
       {
           con.Close();
       }

Something along the lines of:

var memberDetails = GetMemberDetail(12345);
textBox1.Text = memberDetails.Prop1;
textBox2.Text = memberDetails.Prop2;
...

Also I would refactor this method and make sure that I properly dispose disposable resources by wrapping them in using statements to avoid leaking unmanaged handles:

public MemberDetails GetMemberDetail(int membershipgen)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    using (SqlCommand cmd = con.CreateCommand())
    {
        con.Open();
        cmd.CommandText = "usp_getmemberdetail";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@MEMBERSHIPGEN", membershipgen);
        using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
        {
            if (reader.Read())
            {
                return new MemberDetails(
                    reader.GetInt32(reader.GetOrdinal("MEMBERSHIPGEN")), 
                    reader.GetString(reader.GetOrdinal("MEMBERSHIPID")),
                    reader.GetString(reader.GetOrdinal("LASTNAME")),
                    reader.GetString(reader.GetOrdinal("FIRSTNAME")),
                    reader.GetString(reader.GetOrdinal("SUFFIX")),
                    reader.GetString(reader.GetOrdinal("MEMBERTYPESCODE"))
                );
            }
            return null;
        }
    }
}

Get the MemberDetails;

var memberDetails = GetMemberDetail(1);

Populate the textbox;

TextBox.Text = memberDetails.Property;

Jawaid outside of the correct answers that were provided below I would also set SqlConnection con = null; and

SqlCommand cmd = null; outside the try and inside the try put the following 
con = new SqlConnection(connectionString); 

this way if there is an Error when doing cmd.Parameters.Add -- you can trap that exception also dispose of the reader object

if (reader != null)
{
  ((IDisposable)reader).Dispose(); 
  // somthing like that .. do the same for con and cmd objects or wrap them in a using() {}
}

cmd = new SqlCommand("usp_getmemberdetail", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@MEMBERSHIPGEN", SqlDbType.Int, 5));
cmd.Parameters["@MEMBERSHIPGEN"].Value = membershipgen; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM