cerating datatable and then insert it into dataset for given code

Last post 12-28-2007 10:19 AM by LSU.Net. 1 replies.

Sort Posts:

  • cerating datatable and then insert it into dataset for given code

    12-28-2007, 9:34 AM
    • Loading...
    • sansat6699
    • Joined on 07-28-2007, 7:06 AM
    • Posts 61

     hi

    i have code like given below.  i am using asp.net2.0(c#), sqlserver 2000

     public void  loadmunitcombo(ComboBox cmbunit,string sFilter)
        {
            
            DataSet ds1 = new DataSet();
            DataTable  munit1 = new DataTable("munit1");
            string conStr = ConfigurationSettings.AppSettings["asccon"];
            System.Data.SqlClient.SqlConnection dbCon = new System.Data.SqlClient.SqlConnection(conStr);
            System.Data.SqlClient.SqlConnection dbCon1 = new System.Data.SqlClient.SqlConnection(conStr);
            dbCon.Open();
            
            SqlDataReader dr;
            SqlCommand cmd = new SqlCommand("select * from MUTran where M_ID='" + sFilter + "'",dbCon);
            dr = cmd.ExecuteReader();
            
            while (dr.Read())
            {
                string str1;
                dbCon1.Open();
                SqlDataReader dr1;
                SqlCommand cmd1 = new SqlCommand("select * from UM where U_Id="+dr.GetInt32(2)+"",dbCon1);
                dr1 = cmd1.ExecuteReader();
                while (dr1.Read())
                {
                    //here i want to insert data in to datatable one by one and after completion of while loop i want to insert table into dataset
                    ListItem li = new ListItem();
                    li.Text = dr1.GetString(1);
                    li.Value = dr1.GetInt32(0).ToString();
                    
                    cmbunit.Items.Add(new ComboBoxItem(li.Text.ToString()));
                   
                
                }
                dr1.Close();
                dbCon1.Close();
    //here i want to create dataset fro created table above
                
            }
    
            dr.Close();
            dbCon1.Close();
        }

      so please help me in creating datatable and then insert datatable into dataset.

    thank you, 

     

  • Re: cerating datatable and then insert it into dataset for given code

    12-28-2007, 10:19 AM
    Answer
    • Loading...
    • LSU.Net
    • Joined on 03-08-2007, 12:56 PM
    • Louisiana
    • Posts 459

     Using a DataAdapter you could simply fill the dataset and a table would automatically get created with the proper schema.

     

      DataSet ds = new DataSet();
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * from table", cn);
            da.fill(ds)
      


    Please remember to "mark as answered" posts that have helped you.

    -----
    http://lsudotnet.blogspot.com
Page 1 of 1 (2 items)