Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Anyone please help
when form loaded, i want to show first record from database to textbox.
i am using below code but nothing is retrieve when form loaded.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
    {
        protected SqlConnection conn;
        protected SqlCommand command;
        protected SqlDataAdapter adp;
        protected System.Data.DataTable tbl;
     public Form1()
        {
            InitializeComponent();
        }
       
         private void Form1_Load(object sender, EventArgs e)
        {
           
            conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\manish\Documents\info.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            conn.Open();
            adp = new SqlDataAdapter("select * from table1", conn);
            adp = new SqlDataAdapter(command);
            adp.Fill(tbl);
            textBox1.Text = tbl.Rows[0].ToString();
            textBox2.Text = tbl.Rows[1].ToString();
            textBox3.Text = tbl.Rows[2].ToString();
         }
Posted
Updated 15-Mar-14 2:04am
v2
Comments
Please debug and find the issue.

Well...there are some good reasons why that won't work...
1) Your SqlCommand command is never initialized, so your DataAdapter doesn't have a SELECT command.
2) Your DataTable is never initialised, so you can't Fill it.
3) DataRow does not implement ToString, so you will load the string "System.Data.DataRow" into each Textbox...
Try this:
C#
conn = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True");
conn.Open();
command = new SqlCommand("select * from table1", conn);
adp = new SqlDataAdapter(command);
tbl = new DataTable();
adp.Fill(tbl);
textBox1.Text = tbl.Rows[0][0].ToString();

But...you should also Dispose all of those when you have finished with them!
 
Share this answer
 
there exists a basic example here[^]

and msdn entry for SqlDataAdapter[^]

moreover, you can use your debugger to see what happens. to do that:
1- put your breakpoints (F9)
2- start your application in debug (F5)
3- when hits your breakpoint go step by step with F10
4- you can use quick watch for values of your variables.
 
Share this answer
 
try this
-----------------------------------------
textBox1.Text = tbl.Rows[0][0].ToString();
textBox2.Text = tbl.Rows[1][0].ToString();
textBox2.Text = tbl.Rows[2][0].ToString();
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//-------------------------------- sql 
using System.Data;
using System.Data.SqlClient;
//--------------------------------

namespace shoping
{


    class dbconnection

    {
        //-----Connecting To Database
        SqlConnection connect = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Shop;Integrated Security=true");
        SqlDataAdapter da = new SqlDataAdapter();

 

        public DataTable select1(string _Query)
        {
          
                DataTable dt = new DataTable();
                connect.Open();
                da.SelectCommand = new SqlCommand();
                da.SelectCommand.Connection = connect;
                da.SelectCommand.CommandText = _Query;
                da.Fill(dt);
                connect.Close();
                return dt;
           
       
        }


     }
}
//----------------------------------------------------------------------------------------

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
    //-------------------------------- sql    
    using System.Data;
    using System.Data.SqlClient;
    //--------------------------------

   namespace shoping
    {
      class FactorforushDAL
      {

        dbconnection _dbconnect = new dbconnection();

       
         public string createnewfactor()
           {
            DataTable dt = new DataTable();
            dt = _dbconnect.select1("select  isnull( MAX(FactorForushID)+1,1) as newfactorID from TblFactorForush");
            DataRow drow = dt.Rows[0];
            return drow["newfactorID"].ToString();

           


            }

   }
}



//--------------------------------------------------------------------------------




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace shoping
{
    class FactorforushBll
    {
        //----------------------------------------------------------//
        FactorforushDAL _FactorforushDAL = new FactorforushDAL();   //
        //----------------------------------------------------------//




        //------------------------------------------------------
        public string getNewfactorid()
        {
            DataTable dt = new DataTable();
             _FactorforushDAL.createnewfactor();
            DataRow drow = dt.Rows[0];
            return drow["newfactorID"].ToString();

        }
        //------------------------------------------------------
    }
 
}
//-------------------------------------------------------------------------------------


//------------form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shoping
{
    public partial class frmfactorforush : Form
    {

        FactorforushBll _FactorforushBll = new FactorforushBll();
      

    
        public frmfactorforush()
        {
            InitializeComponent();
        }



 private void frmfactorforush_Load(object sender, EventArgs e)
        {
            lblfactoridd.Text = _factorforushBLL.getNewfactorid();
        }

   }
}
//--------------------------------------------------------------------


//---------Script Database
USE [Shop]
GO

/****** Object:  Table [dbo].[TblFactorForush]    Script Date: 03/18/2014 14:20:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TblFactorForush](
	[FactorForushID] [bigint] NOT NULL,
	[Tarikh] [varchar](8) NULL,
	[Saat] [varchar](50) NULL,
	[ForushandehID] [int] NULL,
	[JameKol] [bigint] NULL,
	[jamepardakhtenaghni] [bigint] NULL,
	[MoshtariID] [bigint] NULL,
 CONSTRAINT [PK_TblFactorForush] PRIMARY KEY CLUSTERED 
(
	[FactorForushID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900