Is it possible to work on dataset instead of database?

Last post 11-13-2009 2:01 AM by rush.svadi. 29 replies.

Sort Posts:

  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 5:14 AM
    • Contributor
      3,278 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 10:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    as for as my thinking the problem in this code

    rush.svadi:

    ds = _datatable.Select(exp);

    DataTable dtt = new DataTable();

    dtt.Rows.Add(ds);

    are you sure your ds (DataSet) has proper values (More Than one row).

    if yes then try like this 

    foreach (DataRow theRow in thisDataSet.Tables["Employee"].Rows)
             {
                Console.WriteLine(theRow["ID"] + "\t" + theRow["FirstName"]);
             }

    foreach (DataRow theRow in ds.Tables[0].Rows)

             {

               // here create new datatable and insert record line by line.

             }


    RajneesH Verma
    Sr. Software Developer
    09839040266
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 5:29 AM

    another way is there.. try the below way.

               DataTable dt = //your datatable;

     

     DataView dv = dt.DefaultView;
                dv.RowFilter = "No = 1";
                gvData.DataSource = dv.ToTable();
                gvData.DataBind();

     

                 DataView dv = dt.DefaultView;

                dv.RowFilter = "your expression that you write in select function";

                gvData.DataSource = dv.ToTable();

                gvData.DataBind();

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 5:41 AM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    yes i am sure it will return me more than one row. Your snippet is not working.

    Thanks. Please look at my earlier post for the corresponding logic as suggested by u.

    Thanks

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 6:17 AM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    ramireddyindia:

    another way is there.. try the below way.

               DataTable dt = //your datatable;


     DataView dv = dt.DefaultView;
                dv.RowFilter = "No = 1";
                gvData.DataSource = dv.ToTable();
                gvData.DataBind();


                 DataView dv = dt.DefaultView;

                dv.RowFilter = "your expression that you write in select function";

                gvData.DataSource = dv.ToTable();

                gvData.DataBind();


    Sorry it is not working.......... Still I am getting the empty gridview.......

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 7:06 AM
    • Contributor
      3,278 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 10:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    if you are sure your dataset have those record which you want. Then try this surely helps you

     SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Str"].ToString());

        SqlDataAdapter da;

        DataTable dt;

     protected void Button1_Click(object sender, EventArgs e)
        {       
            da = new SqlDataAdapter("select * from Test1",con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dt = new DataTable();
            createtable();
            int i =0;
            foreach (DataRow theRow in ds.Tables[0].Rows)
            {
                DataRow drow;
                drow = dt.NewRow();
                drow["id"] = ds.Tables[0].Rows[i][0].ToString();
                drow["name"] = ds.Tables[0].Rows[i][1].ToString();
                dt.Rows.Add(drow);          
                i++;
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        public void createtable()
        {
            DataColumn myDataColumn;
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "id";
            dt.Columns.Add(myDataColumn);
           
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "name";
            dt.Columns.Add(myDataColumn);
        } 

     protected void Button1_Click(object sender, EventArgs e)

        {       

            da = new SqlDataAdapter("select * from Test1",con);

            DataSet ds = new DataSet();

            da.Fill(ds);

          /// Above you should use your code which give multiple values in dt. Then use below code

            dt = new DataTable();

            createtable();

            int i =0;

            foreach (DataRow theRow in ds.Tables[0].Rows)

            {

                DataRow drow;

                drow = dt.NewRow();

                drow["id"] = ds.Tables[0].Rows[i][0].ToString();

                drow["name"] = ds.Tables[0].Rows[i][1].ToString();

                dt.Rows.Add(drow);          

                i++;

            }

            GridView1.DataSource = dt;

            GridView1.DataBind();

        }

        public void createtable()

        {

            DataColumn myDataColumn;

            myDataColumn = new DataColumn();

            myDataColumn.DataType = Type.GetType("System.String");

            myDataColumn.ColumnName = "id";

            dt.Columns.Add(myDataColumn);

           

            myDataColumn = new DataColumn();

            myDataColumn.DataType = Type.GetType("System.String");

            myDataColumn.ColumnName = "name";

            dt.Columns.Add(myDataColumn);

        } 


    // I have create two rows only for testing.


    Thanks..



    RajneesH Verma
    Sr. Software Developer
    09839040266
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
  • Re: Is it possible to work on dataset instead of database?

    11-07-2009, 7:24 AM
    • Contributor
      3,278 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 10:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    rush.svadi

    Try as above surely helps you.........


    Thanks..



    RajneesH Verma
    Sr. Software Developer
    09839040266
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
  • Re: Is it possible to work on dataset instead of database?

    11-08-2009, 3:10 AM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    Thanks for the code.... Rajneesh..

    But it not working in my scenario.. 

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-08-2009, 4:13 AM
    • Contributor
      3,278 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 10:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    Till now as you told.

    i think it can be fulfill your requirement if you will try it.

    as i know????


    Thanks..

      

    RajneesH Verma
    Sr. Software Developer
    09839040266
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
  • Re: Is it possible to work on dataset instead of database?

    11-08-2009, 6:50 AM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

     Sorry Rajneesh...... I didnt get u what u r saying....i didnt understand meaning of the that sentence.

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-12-2009, 2:13 AM

    rush.svadi:

    I have a gridview which shows the data from database after firing the search query. I want to implement mulple level filter with two dropdownlist over a gridview. So after returning the dataset from my first search query operation can i only play with the dataset to get further multiple searches instead of going back to database?

    If you want the complete scenario you can view the following post:

    http://forums.asp.net/t/1489776.aspx

    Thanks for yor help.

    Hi Rhishikesh,

    I have read both of your threads and I think you can refer to the following sample:

    WebForm1.aspx

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
    </asp:DropDownList>


    WebForm1.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["northwindConnectionString"].ToString()))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT ProductID,ProductName,CategoryID,SupplierID FROM Products", conn);
    
                DataSet ds = new DataSet();
    
                da.Fill(ds);
    
                DropDownList1.DataSource = ds.Tables[0].Columns;
                DropDownList1.DataBind();
    
                ViewState["dtProducts"] = ds.Tables[0];
    
                DropDownList2Bind();
                GridViewBind();
            }
        }
    
    }
    
    protected void DropDownList2Bind()
    {
        DataTable dt = (DataTable)ViewState["dtProducts"];
    
        DropDownList2.DataSource = dt.DefaultView.ToTable(true, DropDownList1.SelectedValue);
        DropDownList2.DataTextField = DropDownList1.SelectedValue;
        DropDownList2.DataBind();
    }
    
    protected void GridViewBind()
    {
        DataTable dt = (DataTable)ViewState["dtProducts"];
    
        string strExpression;
    
        strExpression = DropDownList1.SelectedValue + " = '" + DropDownList2.SelectedValue + "'";          
    
        dt.DefaultView.RowFilter = strExpression;
        GridView1.DataSource = dt;
        GridView1.DataBind();            
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2Bind();
        GridViewBind();
    }
    
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewBind();
    }


    In your case, please build the filter expression according to the data types of columns :

    DataColumn.Expression Property
    http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

    Jian Kang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Is it possible to work on dataset instead of database?

    11-12-2009, 7:58 AM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    @ Jian Kang........ Thank You very much..... The trick of Viewstate worked for me.....

    Now please suggest me how to implement the same on the already filtered data..... 

    Thanks once again.........

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-12-2009, 9:37 PM

    rush.svadi:
    Now please suggest me how to implement the same on the already filtered data..... 

    Could you please elaborate a bit? I think the sample satisfies your requirements. Have you tried the sample? You may need to make some changes in your scenario.

    DropDownList1 lists all column names in the SELECT query. Once we select a column name in DropDownList1, DropDownList2 will list all distinct values for that column. After we choose a specific value, the GridView will display the filtered DataTable according to the selections of DropDownList1 and DropDownList2.

    rush.svadi:
    Ram.. thanks
    But thats not working becoz the query returns multiple rows. I am getting a page with no gridview at all.
    I want to like this(I am not sure whether my logic is correct). for reference please have look at above code.
    1. Retrieve a DataTable _datatable from database.
    2. Fire a select query to _datatable to return DataRow[] ds. here i may get multiple rows.
    3. Convert DataRow[] to DataTable dtt.
    4 And bind dtt to gridview1.

    I think there is no need add DataRow[] to a DataTable since we can get the filtered DataTable setting the DataTable.DefaultView.RowFilter property and bind it to the GridView control.

    Please refer to the following documents for more details:

    DataTable.DefaultView Property
    http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview.aspx

    DataView.ToTable Method
    http://msdn.microsoft.com/en-us/library/system.data.dataview.totable.aspx

    DataView.RowFilter Property
    http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

    Jian Kang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Is it possible to work on dataset instead of database?

    11-12-2009, 11:19 PM
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    Thanks Jian, The example u gave, helped me at my first stage.

    Let me elaborate with the example:

    Here is my database 

    Code Number       Family      Version      Flag

    1234                        Ge             Ho              A

    1234A                      Ge             Ms              C

    1253                        Sh              Ho              A

    1121                       Sh                Ho             C


    Now if the ddl1 = Version and ddl = Ho; The output will be:


    Code Number       Family      Version      Flag

    1234                        Ge             Ho              A

    1253                        Sh              Ho              A

    1121                       Sh                Ho             C


    Now we get a filtered data. right? Suppose again I want to apply filter on this filter data Lets say: ddl1 = Family ddl2 = sh

    The output of 2nd query has to be like this.


    Code Number       Family      Version      Flag

    1253                        Sh              Ho              A

    1121                       Sh                Ho             C


    Now again suppose i want to filter the above data with ddl1 = flag ddl2 = A. The outout has to be:

    Code Number       Family      Version      Flag

    1253                        Sh              Ho              A


    In this way the process should go on. Every the data must be filtered on the basis of last filter. i.e. what i said multiple times. At every time the data in ddl1 will be same but ddl2 will be different and has to change accordingly. I a working on VS 2005 and .Net 2.0. I hope u understood my actual problem now.

    Thanks once again......

    Thanks and Regards,

    Rhishikesh.
  • Re: Is it possible to work on dataset instead of database?

    11-13-2009, 1:24 AM
    Answer

    OK, I got you.

    Please see:

    T-SQL

    CREATE TABLE Table_7 (CodeNumber VARCHAR(10),Family VARCHAR(10),Version VARCHAR(10),Flag VARCHAR(10))

    INSERT INTO Table_7 VALUES ('1234','Ge','Ho','A')
    INSERT INTO Table_7 VALUES ('1234A','Ge','Ms','C')
    INSERT INTO Table_7 VALUES ('1253','Sh','Ho','A')
    INSERT INTO Table_7 VALUES ('1121','Sh','Ho','C')


    WebForm1.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDB1ConnectionString"].ToString()))
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT CodeNumber,Family,Version,Flag FROM Table_7", conn);
    
                DataSet ds = new DataSet();
    
                da.Fill(ds);
    
                ViewState["dt"] = ds.Tables[0];
                GridViewBind();
    
                DropDownList1Bind();
                DropDownList2Bind();
            }
        }
    }
    
    protected void DropDownList1Bind()
    {
        DataTable dt = (DataTable)ViewState["dt"];
    
        DropDownList1.DataSource = dt.Columns;
        DropDownList1.DataBind();
    }
    
    protected void DropDownList2Bind()
    {
        DropDownList2.Items.Clear();
    
        if (DropDownList1.SelectedValue != "All headers")
        {
            DataTable dt = (DataTable)ViewState["dt"];
    
            if (ViewState["Expression"] != null)
            {
               dt.DefaultView.RowFilter = ViewState["Expression"].ToString();
            }
    
            DropDownList2.DataSource = dt.DefaultView.ToTable(true, DropDownList1.SelectedValue);
            DropDownList2.DataTextField = DropDownList1.SelectedValue;
            DropDownList2.DataBind();              
        }
    
        DropDownList2.Items.Insert(0, "All data");  
    }
    
    protected void GridViewBind()
    {
        DataTable dt = (DataTable)ViewState["dt"];
    
        if (DropDownList1.SelectedValue != "All headers" && DropDownList2.SelectedValue != "All data")
        {
            string strExpression;
    
            if (ViewState["Expression"] != null)
            {                    
                strExpression = ViewState["Expression"].ToString() + " AND " + DropDownList1.SelectedValue + " = '" + DropDownList2.SelectedValue + "'";
            }
            else
            {
                strExpression = DropDownList1.SelectedValue + " = '" + DropDownList2.SelectedValue + "'";
            }
    
            dt.DefaultView.RowFilter = strExpression;
            ViewState["Expression"] =  strExpression;
                        
            DropDownList1.Items.Remove(DropDownList1.SelectedValue);
            DropDownList1.SelectedIndex = 0;
    
            DropDownList2Bind();
        }
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2Bind();            
    }
    
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewBind();
    }

    Jian Kang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Is it possible to work on dataset instead of database?

    11-13-2009, 2:01 AM
    Answer
    • Member
      124 point Member
    • rush.svadi
    • Member since 11-06-2009, 12:17 PM
    • Pune
    • Posts 86

    Thanks Jian....... It is working..... Too good.........Smile


    Mean while I found another solution.......

    Instead of ViewState I used Session. So ddl2_SelectedIndexChange becomes now:


    DataTable dt = (DataTable)Session["Components"];
            string exp;
            if (Session["Components"] != null)
            {
                Session["Components"] = null;
            }        
            exp = "" + ddlheaders.SelectedValue + " like '" + ddldata.SelectedValue + "'";
            dt.DefaultView.RowFilter = exp;
            dt.AcceptChanges();
            GridView1.Visible = true;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session["Components"] = dt.DefaultView.ToTable();
            DataTable dt = (DataTable)Session["Components"];
            string exp;
            if (Session["Components"] != null)
            {
                Session["Components"] = null;
            }        
            exp = "" + ddl1.SelectedValue + " like '" + ddl2.SelectedValue + '";
            dt.DefaultView.RowFilter = exp;
            dt.AcceptChanges();
            GridView1.Visible = true;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Session["Components"] = dt.DefaultView.ToTable();


    Thanks.... Jian Once again....... Thanks for helping me...........Smile

    Thanks and Regards,

    Rhishikesh.
Page 2 of 2 (30 items) < Previous 1 2