locked
Data Table problem RRS feed

  • Question

  • User567064909 posted

    salam all ..

    i have the following code :

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string a,b,c,d;
                DataTable dtBasicData = new DataTable();
                dtBasicData.Clear();
                dtBasicData = DataEntry.GetBasicDataByYear(25);
                a = dtBasicData.Rows[1][1].ToString();
                b = dtBasicData.Rows[1][2].ToString();
                c = dtBasicData.Rows[1][3].ToString();
                d = dtBasicData.Rows[1][4].ToString();
                dtBasicData.Clear();
            }
        }

    i use SQL server database .. table Years has only one record (only one) when i execute the above code it work good (dtBasicdata.Rows[1][1].ToString()) but sometimes give me error so i change to (dtBasicdata.Rows[0][1].ToString()) but i get null values .. how this come????

    Sunday, April 26, 2015 11:19 PM

Answers

  • User753101303 posted

    Hi,

    it print information from users table

    If you meant that you don't always see the column you expect, more likely this is because your data access layer uses static member. A static member is shared by the whole application which causes issues if several users are using the same data at the same time (ie user A define a shared DataTable to a value, user B code defines the same DataTable to another value and user A finally sees what should have been returned to user B.

    If this is not that try to elaborate what is null (is this because you don't find the column which would still point to a static data issue or because the column value itself is null which can happen depending on what your query does).

    As for now it seems to return quite unexpected results it would point to a static data issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 27, 2015 7:37 AM

All replies

  • User-1716253493 posted

    wa 'alaikum salam

    if you only have one records and 4 columns

    I guess the code should looks like this

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string a,b,c,d;
                DataTable dtBasicData = new DataTable();
                dtBasicData.Clear();
                dtBasicData = DataEntry.GetBasicDataByYear(25);
                a = dtBasicData.Rows[0][0].ToString();
                b = dtBasicData.Rows[0][1].ToString();
                c = dtBasicData.Rows[0][2].ToString();
                d = dtBasicData.Rows[0][3].ToString();
                dtBasicData.Clear();
            }
        }

    Sunday, April 26, 2015 11:43 PM
  • User567064909 posted

    Thank you for ur reply .. i said before i change to index [0] i get null .. my page was work correctly but this problem appear suddenly .. i rewrite the code again.. i check everything .. the problem still happend .. and some times i get different results with same code in different times ...  

    Monday, April 27, 2015 12:34 AM
  • User567064909 posted

    something going wrong ... see the following example:

    I use this code in one separate page to test and solve this problem:

    DataTable dt = new DataTable();
    dt = QA.ProjectLib.Years.GetLastAdded();
    Response.Write(dt.Rows[0]["Year"].ToString());

    when i run my web application it print information from users table!!! what is happened?? Is it cache or what? how i can solve this problem. I am sure that i write everything correctly.

    Monday, April 27, 2015 5:56 AM
  • User753101303 posted

    Hi,

    it print information from users table

    If you meant that you don't always see the column you expect, more likely this is because your data access layer uses static member. A static member is shared by the whole application which causes issues if several users are using the same data at the same time (ie user A define a shared DataTable to a value, user B code defines the same DataTable to another value and user A finally sees what should have been returned to user B.

    If this is not that try to elaborate what is null (is this because you don't find the column which would still point to a static data issue or because the column value itself is null which can happen depending on what your query does).

    As for now it seems to return quite unexpected results it would point to a static data issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 27, 2015 7:37 AM
  • User567064909 posted

    Hi PatriceSc ... I think you are right .. I use static keyword ... the following is my complete code:

    DataAccess class:

    public class DB
        {
            public static SqlConnection conn = new SqlConnection(@"data source=.\SQLEXPRESS ; database=MyDB; integrated security=true");
            static SqlCommand cmd = new SqlCommand();
            static DataTable dt = new DataTable();
    
            public static bool sqlExecute(string sql)
            {
                conn.Open();
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                try
                {
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    return true;
                }
                catch
                {
                    conn.Close();
                    return false;
                }
            }
    
    
            public static DataTable sqlRead(string sql)
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(dt);
                return dt;
            }
        }

    Here is Year class:

    public class Years
       {
             public static DataTable GetAll()
            {
                return DB.sqlRead("select * from Years order by y_ID DESC");
            }
    
            public static DataTable GetLastAdded()
            {
                return DB.sqlRead("select top 1 * from Years order by y_ID DESC");
            }
    
        }

    And in my webpage code behind i write the following code:

    DataTable tdYears = new DataTable();
    tdYears = Years.GetAll();
    Gridview1.DataSource = tdYears;
    Gridview1.DataBind();

    Sometimes when i execute this code it gives me another data from another table .. and sometimes Gridview display many many repeated and null values that is not in table .. 

    my question now what is the reason? and you mean static members is what i write above in my code?

    Thank you alot for your help ..

    Tuesday, April 28, 2015 1:08 AM
  • User-1716253493 posted

    There is something error inside GetBasicDataByYear(25) i guess.

    Time out error or something.

    Tuesday, April 28, 2015 2:19 AM
  • User567064909 posted

    Thank you  oned_gk .. 

    I found the problem .. all it is related to static member of my data access layer ...

    i really appreciate your help and i hope you get great time ... Wink 

    Tuesday, April 28, 2015 3:08 AM
  • User567064909 posted

    Thank you PatriceSc ..

    I just change :

    static DataTable dt= new DataTable();

    to be as :

    DataTable dt= new DataTable();

    All thing is going well now ...

    thaaaaaank you ..

    Tuesday, April 28, 2015 3:10 AM