C# App:how to fetch records from the database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muskan
    New Member
    • Nov 2006
    • 35

    C# App:how to fetch records from the database

    hello,

    Can u tell me how we can retrieve the records from the database.
  • vinaykeshav
    New Member
    • Dec 2006
    • 25

    #2
    [CODE=cpp]connection = new SqlConnection() ;
    connection.Conn ectionString = "Persist Security Info=False;" +
    "Integrated Security=SSPI;d atabase=northwi nd;" +
    "server=localho st;Connect Timeout=30";
    connection.Open ();
    dataadapter = new SqlDataAdapter( "select * from customers", connection);
    dataset = new DataSet();
    dataadapter.Fil l(dataset, "table1");
    dataGrid1.DataS ource = dataset;
    dataGrid1.DataM ember = "table1";[/CODE]
    Last edited by Shashi Sadasivan; Nov 26 '07, 10:04 PM. Reason: adding code tags

    Comment

    • muskan
      New Member
      • Nov 2006
      • 35

      #3
      Actually I want to know how we can fetch records from database into controls using dataadapter not execute reader command.

      Comment

      • vinaykeshav
        New Member
        • Dec 2006
        • 25

        #4
        in the example i have provided it uses the dataadapter itself,
        and datagrid is a control where the data is getting binded
        Originally posted by muskan
        Actually I want to know how we can fetch records from database into controls using dataadapter not execute reader command.

        Comment

        • muskan
          New Member
          • Nov 2006
          • 35

          #5
          I want to put data into textboxes not datagrid

          Comment

          • vinaykeshav
            New Member
            • Dec 2006
            • 25

            #6
            for a textbox i am sure that u wil not fetch more than one row of data
            so,,..
            [CODE=cpp]connection = new SqlConnection() ;
            connection.Conn ectionString = "Persist Security Info=False;" +
            "Integrated Security=SSPI;d atabase=northwi nd;" +
            "server=localho st;Connect Timeout=30";
            connection.Open ();
            dataadapter = new SqlDataAdapter( "select * from customers", connection);
            dataset = new DataSet();
            dataadapter.Fil l(dataset, "table1");
            //dataGrid1.DataS ource = dataset;
            //so once ur dataset is filled then from that pick one value
            textbox1.text = dataset.tables( "table1").colum ns("colname");[/CODE]

            Originally posted by muskan
            I want to put data into textboxes not datagrid
            Last edited by Shashi Sadasivan; Nov 26 '07, 10:05 PM. Reason: adding code tags

            Comment

            • muskan
              New Member
              • Nov 2006
              • 35

              #7
              I have written like this but it shows column name into the textboxes.
              means it is showing name & roll no in textboxes instead of data values.

              txtname.Text =ds.Tables[0].Columns["name"].ToString() ;
              txtroll.Text= ds.Tables[0].Columns["roll no"].ToString() ;
              OR

              txtname.Text =ds.Tables["deatail].Columns["name"].ToString() ;
              txtroll.Text= ds.Tables["detail"].Columns["roll no"].ToString() ;

              Comment

              • vinaykeshav
                New Member
                • Dec 2006
                • 25

                #8
                txtname.Text =ds.Tables[0].ds.Tables["details"].Rows[0]["name"].ToString() ;
                txtname.Text =ds.Tables[0].ds.Tables["details"].Rows[0]["roll no"].ToString() ;


                please specify the row no like zeroth ..

                Originally posted by muskan
                I have written like this but it shows column name into the textboxes.
                means it is showing name & roll no in textboxes instead of data values.

                txtname.Text =ds.Tables[0].Columns["name"].ToString() ;
                txtroll.Text= ds.Tables[0].Columns["roll no"].ToString() ;
                OR

                txtname.Text =ds.Tables["deatail].Columns["name"].ToString() ;
                txtroll.Text= ds.Tables["detail"].Columns["roll no"].ToString() ;

                Comment

                • muskan
                  New Member
                  • Nov 2006
                  • 35

                  #9
                  thanks ...........a lot

                  now it is working.

                  but we can see only one record at a time.
                  how we can see all records one by one.

                  Comment

                  • vinaykeshav
                    New Member
                    • Dec 2006
                    • 25

                    #10
                    to view the list u need to use the listbox / datagrid ,,, u can display all items inside the textbox using a comma seperator...
                    Originally posted by muskan
                    thanks ...........a lot

                    now it is working.

                    but we can see only one record at a time.
                    how we can see all records one by one.

                    Comment

                    • childg
                      New Member
                      • Nov 2007
                      • 2

                      #11
                      I tried the code above and it tells me that it can't connect to the server.

                      "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections."

                      but i have my db local in the app_data folder.

                      Comment

                      • Shashi Sadasivan
                        Recognized Expert Top Contributor
                        • Aug 2007
                        • 1435

                        #12
                        please refer to www.connections trings.com on how to create a connection string to your database (file / server based)

                        Comment

                        • childg
                          New Member
                          • Nov 2007
                          • 2

                          #13
                          edit: after like 2 hours of sitting debugging i figure out my select statement had a missing space between the quotes...gotta love those buggers.

                          Comment

                          • ashish711
                            New Member
                            • Mar 2008
                            • 1

                            #14
                            I Have To Insert Data In Sql Server Through Textbox WHEN CLICK ON BUTTON In Database Connectivity Plz Help Me

                            Comment

                            Working...