1

I can only read from gridview. I want to pass the read file from the textbox upon the user logs in..

Here is my Data Access Tier

public static SqlDataReader GetInformation(string Username)
{
    string sql = "Select Idno, Lname, Mname, Username, Password, ConPass, SchoolYear, TermGraduated from Graduates where Username = @username";
    Open();
    SqlCommand com = new SqlCommand(sql, con);
    com.Parameters.AddWithValue("@username", Username);
    com.CommandType = CommandType.Text;
    SqlDataReader sdr = com.ExecuteReader();
    Close();
    return sdr;    
}

And here is my Bussiness Logic Layer

public IDataReader GetInformation()
{
    return DAT.GetInformation(this.username);
}

Then my presentation layer in form_load

b.Username = Session["Username"].ToString();

How can I transfer the data from the textboxes? The only thing I can do is to transfer it using a gridview. But how can I all transfer the data from the textboxes like "TextBox1, TextBox2, TextBox3, TextBox4, TextBox5..." And so on

7
  • 4
    "How can I transfer the data from the textbox?" What textbox? Commented Aug 19, 2014 at 13:22
  • Updated my question. It should be textboxes. How can i populate the textboxes based on that statement. Commented Aug 19, 2014 at 13:26
  • Where are the text-boxes?? Or you just trying to fill a gridview? do you have textboxes in template fields Commented Aug 19, 2014 at 13:27
  • @JmSantos even with update I cannot understand: what "textboxes"? And the "from" word sounds strange in this sentence. Could you clarify? Commented Aug 19, 2014 at 13:27
  • @JmSantos: And this way of coding, where is the data-reader get closed? Commented Aug 19, 2014 at 13:28

1 Answer 1

1

So if I understood your question

Create a class to represent data, let say Graduate

public class Graduate
{
     public long Id {get;set;}
     public string LastName {get;set;}
     //rest of the fields
}

public static Graduate GetInformation(string Username)
{

//haven't put all the code here, you have the idea I guess
//also wrap this around try-catch block


    string sql = "Select Idno, Lname, Mname, Username, Password, ConPass, SchoolYear,    TermGraduated from Graduates where Username = @username";
    Open();
    SqlCommand com = new SqlCommand(sql, con);
    com.Parameters.AddWithValue("@username", Username);
    com.CommandType = CommandType.Text;

using(var sdr = com.ExecuteReader())
{
   if(sdr.HasRows)
   {
       while(sdr.Read())
       {
          var objGard = new Graduate()
                     {
                        ID = sdr["Idno"] != DBNull.Value
                                ? long.Parse(sdr["Idno"].ToString())
                                : 0,
                        LastName = reader["Lname"] != DBNull.Value
                                    ? reader["Lname"].ToString()
                                    : ""
                        //rest of the fields
                     };
               return objGard;             
       }
   }
}
return null;

}

public Graduate GetInformation()
{
    return DAT.GetInformation(this.username);
}

var grad = GetInformation();
if(grad ==null) return;

txtId.Text = grad.Id;
txtLasName.Text = grad.LastName;
Sign up to request clarification or add additional context in comments.

11 Comments

Should i put these in my presentation layer?
Shouldn't be! Ideally you should create a Class which represent the data (probably Graduates) in this case and create a class object based on data and return that to presentation layer
I am using 3 tier. How can I return it to let it populate the textboxes?
@humpty dumpty - what is the point of creating a class to represent the data - populating a 'Graduates' object and then using the object as a datasource? This really seems, to me, to be creating a class/object for the sake of it. Why not just use the SqlDataReader as the datasource?
@MartinSmellworse : I initially thought that, but OPs' requirement is not to fill the grid-view, populate some text boxes (read the OPs comment) !!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.