none
Microsoft Access Data in a textbox using C#

    Question

  • Hi, i have completed the connection & all that.

    I am able to access the data in a console application.
    Also i am able to access state change in formation(when database opens & closes) in a text box.

     I just want to access a coloumn value in my textbox.


    Some thing like this : textBox1.Text = "data from the datbase.table.column";

    I mean do we need Dataset, Data Adaptor & all that, Can't we just do it directly.Without all this headache.
    • Moved by RobinDotNetMVP Thursday, August 20, 2009 5:16 AM (From:ClickOnce and Setup & Deployment Projects)
    Thursday, August 20, 2009 4:10 AM

Answers

  • You have to do like this:

    string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";

             string sql = @"select * from employee ";

             SqlConnection conn = new SqlConnection(connString);
             SqlDataAdapter da = new SqlDataAdapter(sql, conn);
             DataSet ds = new DataSet();
             da.Fill(ds, "employee");

             // Bind to FirstName column of the Employees table 
             textBox1.DataBindings.Add("text", ds, "employee.firstname");
             // Bind to LastName column of the Employees table
             textBox2.DataBindings.Add("text", ds, "employee.lastname");

    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Proposed as answer by _SuDhiR_ Thursday, August 20, 2009 12:04 PM
    • Marked as answer by Harry Zhu Thursday, August 27, 2009 1:16 AM
    Thursday, August 20, 2009 5:51 AM
  • Hi
    _SuDhiR_ gave a quite universal and general answer. I can only add, that in general you need DataSet with DataTable, but there are cases when you may need only DataCommand+DataReader or even only DataCommand if you want to get one scalar value.
    • Proposed as answer by Harry Zhu Tuesday, August 25, 2009 6:49 AM
    • Marked as answer by Harry Zhu Thursday, August 27, 2009 1:16 AM
    Thursday, August 20, 2009 6:04 AM
  • Hi,

    I'm afraid these classes such as Data Adaptor , connection , and command are required.
    They are responsible for working with the database for connecting, retrieving, updating , inserting and deleting data.
    Just access data as you did in the console application.

    You might also want to try DataReader which provide forward only , read only access to database.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Tuesday, August 25, 2009 6:49 AM
    • Marked as answer by Harry Zhu Thursday, August 27, 2009 1:16 AM
    Tuesday, August 25, 2009 6:48 AM
  • Hello,

    Well Sahil Ansari, you can have a DAL (Data Access Layer) that simplifies the way you handle the data out and in to the DataBase.

    Here are few useful links for custom made DAL.

    Implement a Data Access Layer for Your App with ADO.NET

    Very Lightweight Data Access Layer in C# and .NET 2.0

    .NET Application Architecture: the Data Access Layer

    Creating a Data Access Layer in .NET

    Microsoft Technologies.

    MSDN: SQL Server Management Objects (SMO)

    SMO - Manage your SQL Server!

    MSDN: The ADO.NET Entity Framework Overview
     

    Mapping Inheritance trees using Entity Framework

    MSDN: LINQ to SQL

    Using LINQ to SQL 


    I prefer using a custom DAL framework using the base ADO.Net classes rather than using Microsoft technologies, but it's not always the case, also you can find alternatives in open source projects that made most of the work for you, here is one System.Data.SQLite.

    Hope it helps.




    Eyal, Regards.

    blog.eyalsh.net
    • Marked as answer by Harry Zhu Thursday, August 27, 2009 1:16 AM
    Tuesday, August 25, 2009 8:09 AM

All replies