ASP.Net,ViewState中的数据表并发用户的问题 [英] ASP.Net , datatable in viewstate Problem for concurrent user

查看:119
本文介绍了ASP.Net,ViewState中的数据表并发用户的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个aspx页面,在其中我在ViewState中添加了一个数据表来访问回发的值.它工作正常.但适用于不同用户使用同一页面的并发事务中.这会带来问题.

前任.用户1-使用sales.aspx(asp.net表单),在其中他可以为一个账单包含多个项目.

对于并发用户-用户2-使用sales.aspx表格,他还可以为其账单添加多个项目.在这里,用户2访问用户1添加的dataTable值.


请帮忙解决这个问题非常迫切.我已经附上了C#的示例代码.

Hi,

i have a aspx page, where i have added a datatable in ViewState to access value for postbacks. it works fine. but for in concurrent transaction where different user uses the same page. it gives problem.

Ex. User 1 - uses sales.aspx(asp.net form) where he can had multiple item for single bill.

for concurrent User - User 2 - uses sales.aspx form he can also add multiple item for his bill. here User 2 accesses dataTable values added by User 1.


Please help out to solve this problem it is very urgent. i have attached sample code of c#.

private void createDynamicGridInvoice()
   {
       try
       {
           invoiceDT = new DataTable();
           invoiceDT.Columns.Add("SlNo", typeof(int));
           invoiceDT.Columns["SlNo"].AutoIncrement = true;
           invoiceDT.Columns["SlNo"].AutoIncrementSeed = 1;
           invoiceDT.Columns["SlNo"].AutoIncrementStep = 1;
           invoiceDT.Columns.Add("stkName");
           invoiceDT.Columns.Add("MRP");
           invoiceDT.Columns.Add("Discount");
           invoiceDT.Columns.Add("Qty");
           invoiceDT.Columns.Add("Rate");
           invoiceDT.Columns.Add("Tax");
           invoiceDT.Columns.Add("Amount");
           invoiceDT.Columns.Add("ItemId");
           ViewState["VWInvTable"] = invoiceDT;
          
       }
       catch
       {
       }
   }







protected void btnAdd_Click(object sender, EventArgs e)
   {
       try
       {
           if (hdnItemId.Value != "")
           {
               lblSaveMsg.Text = "";
               if (txtDiscount.Text == "")
               {
                   txtDiscount.Text = "0";
               }
               double mrp = Convert.ToDouble((txtMRP.Text).ToString());
               double discount = Convert.ToDouble((txtDiscount.Text).ToString());
               gQty = Convert.ToInt32(txtQty.Text);
               if (ddTaxRate.SelectedIndex == 0)
               {
                   gVat = 0;
               }
               else
               {
                   gVat = Convert.ToDouble(ddTaxRate.SelectedItem.ToString());
               }
               calculateExclusiveMrp(mrp, gVat, discount);
               calculateAmount(extax, Convert.ToInt32(gQty));
               DataRow drValues = invoiceDT.NewRow();
               drValues["stkName"] = txtStkItem.Text;
               drValues["MRP"] = mrp;
               drValues["Discount"] = txtDiscount.Text;
               drValues["Qty"] = gQty;
               gRate = extax;
               drValues["Rate"] = string.Format("{0:0.00}", gRate);
               drValues["Tax"] = string.Format("{0:0.00}", gVat);
               drValues["Amount"] = string.Format("{0:0.00}", amount);
               drValues["ItemId"] = hdnItemId.Value;
               ViewState["VWInvTable"] = (DataTable)invoiceDT;
               invoiceDT.Rows.Add(drValues);
               GVSalesInvoice.DataSource = invoiceDT;
               GVSalesInvoice.DataBind();
               if (ddTaxRate.SelectedIndex != 0)
               {
                   if (gVat == Convert.ToDouble(ddTaxRate.SelectedItem.ToString()))
                   {
                       CalculateVAT(gRate.ToString(), gQty.ToString(), gVat.ToString());
                   }
               }
               Clear();
             //  ddTaxRate.SelectedIndex = 2;
               txtStkItem.Focus();
               btnSave.Enabled = true;
           }
       }
       catch (Exception ex)
       {
           errHandler.WriteError(ex.Message.ToString());
       }
   }

推荐答案

您真的不应该在ViewState中存储数据表.它给页面增加了很多额外的东西,这会降低应用程序的性能.您所拥有的是一个购物车,其中有很多示例可用.通常,信息存储在会话状态,Cookie或服务器中缓存的内存数据库中,并通过与用户或会话关联的ID进行访问.

您还可以通过代码进行一些改进.

You really shouldn''t be storing a DataTable in ViewState. It is adding quite a bit extra to your page that will slow performance of your application. What you have is a shopping cart, of which there are many, many examples available. Typically the information is stored in either session state, cookies, or an in-memory database cached at the server and accessed via an id associated with the user or session.

You also have several things that could be improved with your code.

(txtMRP.Text).ToString()



假设txtMRP是文本框控件,则ToString是多余的,因为Text属性始终返回字符串.

在这里,您将值转换为int



Assuming txtMRP is a textbox control then the ToString is redundant since the Text property return a string anyway.

Here you convert a value to an int

gQty = Convert.ToInt32(txtQty.Text);


然后过了几行,再次将int再次转换为int.


Then a few lines later once again convert the int to an int again.

calculateAmount(extax, Convert.ToInt32(gQty));



不要存储格式化的值.格式化应在显示时完成.现在,数据将以不同的方式使用,例如在计算中,您需要将其从字符串转换回其固有类型.



Don''t store formatted values. Formatting should be done at display time. Now it the data were to be used on a different manner, such as in a calculation, you would need to convert it from a string back to its intrinsic type.

drValues["Rate"] = string.Format("{0:0.00}", gRate);



如果您不打算对其进行任何操作,则实现try/catch没有任何意义.如果要隐藏异常,请修复导致异常的问题.



There is no point in implementing try/catch if you are not going to do anything about it. If it is to hide the exception then fix the problem that causes the exception.

catch
{
}


这篇关于ASP.Net,ViewState中的数据表并发用户的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆