Page view counter

How to display data

Last post 01-15-2009 8:33 PM by zoaka. 13 replies.

Sort Posts:

  • How to display data

    01-09-2009, 7:05 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6

     I have a web service method call selectCust and a web project form call updateCust. When the user key in the id of the customer, it will redirect to another page which will have the customer detail display out on the textbox. The question i wanted to ask is how to display out the data to a textbox?

  • Re: How to display data

    01-09-2009, 7:24 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 11,856
    • Points 75,147

    Here's one way:

     

    private void getData(string user)
    {
    DataTable dt = // Set the DataSource here that comes from your Web Service and pass the username as the parameter
    if (dt.Rows.Count > 0)
    {
    TextBox1.Text = dt.Rows[0]["ColumnName1"].ToString(); //Where ColumnName is the Field from the DB that you want to display
    TextBox2.Text = dt.Rows[0]["ColumnName2"].ToString();
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!Page.IsPostBack){
    getData(this.User.Identity.Name);
    }

    }

    You may look here for more info:

    http://geekswithblogs.net/dotNETvinz/archive/2008/09/12/bind-textbox-and-label-control-with-data-from-database.aspx

  • Re: How to display data

    01-09-2009, 8:17 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6
    vinz:


        DataTable dt = // Set the DataSource here that comes from your Web Service and pass the username as the parameter



    For this part i don't understand what to put at the comment part

    vinz:


       if (!Page.IsPostBack){
        getData(this.User.Identity.Name);



    And for this part can explain the this.User.Identity.Name?

    Thanks!!
  • Re: How to display data

    01-14-2009, 2:13 AM
    Answer

    zoaka:
    DataTable dt = // Set the DataSource here that comes from your Web Service and pass the username as the parameter

    Please refer this code,

     

       private static DataTable GetTableByUsername(string UserName)
        {
            DataSet ds = new DataSet();
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnstring"].ToString();
            string queryString = "select * from users where username = '" + UserName + "'";
            GetDatabase(ds, connectionString, queryString);
            return ds.Tables[0];
        }
        private static DataSet GetDatabase(DataSet dataset, string connectionString, string queryString)
        {
            using (SqlConnection connection =
                new SqlConnection(connectionString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = new SqlCommand(
                    queryString, connection);
                adapter.Fill(dataset);
                return dataset;
            }
        }
      

    zoaka:
    this.User.Identity.Name

    Please check this link,

    http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.name.aspx

     

     

    Hong-Gang Chen
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How to display data

    01-14-2009, 7:23 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6

    Hong-Gang Chen - MSFT:

    private static DataTable GetTableByUsername(string UserName) { DataSet ds = new DataSet(); string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconnstring"].ToString(); string queryString = "select * from users where username = '" + UserName + "'"; GetDatabase(ds, connectionString, queryString); return ds.Tables[0]; } private static DataSet GetDatabase(DataSet dataset, string connectionString, string queryString) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand( queryString, connection); adapter.Fill(dataset); return dataset; } }  

     

     

    do i put this part inside my webservice or at the form itself?

  • Re: How to display data

    01-14-2009, 7:44 AM
    • Loading...
    • shahed.kazi
    • Joined on 07-08-2008, 10:15 PM
    • Sydney, Australia
    • Posts 1,876
    • Points 9,189

     If you can select the cust from the database then you can get the necessary information. The redirected page can have the code that can retrieve the data and set it to the text property of the TextBox.

  • Re: How to display data

    01-14-2009, 8:51 AM
    Answer
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 11,856
    • Points 75,147

    zoaka:
    vinz:


        DataTable dt = // Set the DataSource here that comes from your Web Service and pass the username as the parameter



    For this part i don't understand what to put at the comment part
     

    In your Service create a method that returns a DataTable or a DataSet that accepts the userid as the parameter.. something like:

     

    //A method from your Service
    public DataTable GetCustomerInfo(string user)
    {
    	DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"); 
            try
            {
            connection.Open();
    	SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Customers WHERE UserID = @user;", connection);
    	SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    
            sqlCmd.Parameters.AddWithValue("@user",user);
    	sqlDa.Fill(dt);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                    string msg = "Fetch Error:";
                    msg += ex.Message;
                    throw new Exception(msg);
    
            }
            finally
            {
                connection.Close();
            }
    
    	return dt;
    }
    
    //Then In your ASPX Form
    You can reference that method above like below:
    
    private void GetDataFromService(string user)
    {
    
        MyWebService service = new MyWebService();
        DataTable dt = service.GetCustomerInfo(user); // Set the DataSource here that comes from your Web Service and pass the username as the parameter
        if (dt.Rows.Count > 0)
        {
               TextBox1.Text = dt.Rows[0]["ColumnName1"].ToString(); //Where ColumnName is the Field from the DB that you want to display
               TextBox2.Text = dt.Rows[0]["ColumnName2"].ToString();
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
    
       if (!Page.IsPostBack){
        GetDataFromService(this.User.Identity.Name);
       }
    
    }
    You can refer to the following links below for more info:
    http://blogs.interfacett.com/dan-wahlins-blog/2007/3/10/video-creating-web-services-with-the-net-framework.html 
    http://digitalcolony.com/2007/08/consuming-web-service-in-aspnet.aspx 
    http://msdn.microsoft.com/en-us/library/ms972326.aspx 
    zoaka:
    vinz:


       if (!Page.IsPostBack){
        getData(this.User.Identity.Name);



    And for this part can explain the this.User.Identity.Name?
     
    It basically get's the current logged user.. Refer to the link provided by Hong Gang for more information.
  • Re: How to display data

    01-15-2009, 3:15 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6

     erm i am using Microsoft Access database so i just change the SqlConnection to OleDbDataAdapter?

  • Re: How to display data

    01-15-2009, 8:04 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 11,856
    • Points 75,147

    zoaka:
    i am using Microsoft Access database
     

    Use the System.Data.OleDb namespace..

  • Re: How to display data

    01-15-2009, 10:50 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6
     I encounter an error when i run the project "Cannot implicitly convert type 'CustomerWebApp.custWS.getDataResponseGetDataResult' to 'System.Data.DataTable' "

     This are my codes:

    The ASPX form:

     ProcessCustomer processCust = new ProcessCustomer(); //this is my webservice name
           
            protected void Page_Load(object sender, EventArgs e)
            {        
                if (!Page.IsPostBack)
                {
                    getData(this.User.Identity.Name);
                }
            }

            private void getData(string id)
            {
                string cusid = Session["id"].ToString();
                DataTable ddt = processCust.getData(cusid);
                if (ddt.Rows.Count > 0)
                {
                   idTextBox.Text = ddt.Rows[0]["CustomerID"].ToString();
                   fnameTextBox.Text = ddt.Rows[0]["FirstName"].ToString();
                   lnameTextBox.Text = ddt.Rows[0]["LastName"].ToString();
                   emailTextBox.Text = ddt.Rows[0]["Email"].ToString();
                }
            }

    This is in my service where i have a class file:

     public DataTable getData(String CustomerID)
            {
                conn = dbCon.getConnection(); //this is my database connection
                DataTable dt = new DataTable();
                try
                {
                    conn.Open();
                    string sqlCmd = "Select * FROM Customer Where CustomerID = '" + CustomerID + "'";
                    OleDbDataAdapter oleDB = new OleDbDataAdapter(sqlCmd, conn);
                    oleDB.Fill(dt);
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                }

                return dt;
            }

    and this is my web service file, the ASMX file:

     [WebMethod]
            public DataTable getData(string CustomerID)
            {
                return custObj.getData(CustomerID);
            }

    Is there any wrong with the code?
  • Re: How to display data

    01-15-2009, 10:59 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 11,856
    • Points 75,147

    In what line does the error occured? Be sure that you return a method that returns a DataTable when you pass an object to another DataTable..

  • Re: How to display data

    01-15-2009, 11:12 AM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6

     The error occur on

    ProcessCustomer processCust = new ProcessCustomer(); //this is my webservice name
           
            protected void Page_Load(object sender, EventArgs e)
            {        
                if (!Page.IsPostBack)
                {
                    getData(this.User.Identity.Name);
                }
            }

            private void getData(string id)
            {
                string cusid = Session["id"].ToString();
                DataTable ddt = processCust.getData(cusid); //this is the line where the error occur
                if (ddt.Rows.Count > 0)
                {
                   idTextBox.Text = ddt.Rows[0]["CustomerID"].ToString();
                   fnameTextBox.Text = ddt.Rows[0]["FirstName"].ToString();
                   lnameTextBox.Text = ddt.Rows[0]["LastName"].ToString();
                   emailTextBox.Text = ddt.Rows[0]["Email"].ToString();
                }
            }

  • Re: How to display data

    01-15-2009, 11:55 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 11,856
    • Points 75,147
    hummm..What version of .NET you used?
  • Re: How to display data

    01-15-2009, 8:33 PM
    • Loading...
    • zoaka
    • Joined on 01-07-2009, 4:17 AM
    • Posts 22
    • Points 6

     Microsoft Visual Studio 2005

    .NET Framework 2.0

     

Page 1 of 1 (14 items)