【问题标题】:Update a table with the ListBox items in C#用 C# 中的 ListBox 项更新表
【发布时间】:2017-05-12 23:53:34
【问题描述】:

我正在尝试使用列表框项更新表格。我遇到错误“int”不包含“Items”的定义,并且找不到接受“int”类型的第一个参数的扩展方法“Items”(您是否缺少 using 指令或程序集引用?)

感谢您的帮助

HTML

 <asp:ListBox ID="lbSampleID" runat="server" Height="99px" Width="131px">   </asp:ListBox>

<input type="button"  id="btnUpdateConsent" value="Update Consent"     onclick="updateConsent();" class="auto-style142" /> 

JavaScript

<script type="text/javascript">  

         function updateConsent() {

             var SampleID = document.getElementById("lbSampleID").value;
             xmlhttp.open("GET", "UpdateConsent.aspx?mb=" + MBID + " &ci= " + ConsentID + "&cd=" + ConsentDate +
                 "&wd=" + WithdrawDate + "&cw=" + ConsentWithdraw + "&ncn=" + NewConsentName + "&si=" + SampleID, false);

             xmlhttp.send(null);
         }

C#

  string lbSampleID = Request.QueryString["si"].ToString();
 using (SqlCommand sc = new SqlCommand(@"Update Sample
         set ConsentConfirmed='NO' , DateConsent='', ConsentNameID=@NewConsentName

            WHERE  MBID = @MBID and ConsentNameID = @ConsentID and SampleID=@SampleID", con))
                {
                    sc.Parameters.Add("@MBID", SqlDbType.Int).Value=MBID;

                    sc.Parameters.Add("@ConsentID", SqlDbType.Int).Value = ConsentID;
                    sc.Parameters.Add("@NewConsentName", SqlDbType.Int).Value = NewConsentName;

                    int records = sc.ExecuteNonQuery();
                    int value = records;


                    int ListBox1 = Convert.ToInt32(lbSampleID);
            // Error with the loop
                  foreach (ListItem item in ListBox1.Items)
                    {
                        if (item.Selected)
                        {
                            try
                            {
                                sc.Parameters["@SampleID"].Value = item.Text;
                                sc.ExecuteNonQuery();
                            }

                            catch (Exception ex)
                            {
                                // Label1.Text = ex.Message;
                            }

                        }

                    }
                }

【问题讨论】:

  • 为了将来的参考,请参阅这篇关于创建Minimal, Complete, and Verifiable 示例的文章来说明您面临的问题。您发布的大部分代码与您的问题无关。

标签: c# asp.net sql-server


【解决方案1】:

是这样的:

int ListBox1 = Convert.ToInt32(lbSampleID);
            // Error with the loop
                  foreach (ListItem item in ListBox1.Items)

您已经声明了一个名为ListBox1int 并为其分配了一个值。我认为你想要做的更像是这样的:

             int i = Convert.ToInt32(lbSampleID);
 //if lbSampleID is a label, use its .Text Property   
               foreach (ListItem item in ListBox1.Items)
                  if(item.ToString() == i.ToString())
                     {//do things }

但我不能确定发布的示例中的逻辑是什么。

【讨论】:

    【解决方案2】:

    在导致错误的行的正上方,您将ListBox1 定义为int 类型的变量。

    int ListBox1 = Convert.ToInt32(lbSampleID);
    

    因此,编译器会抛出一个错误,告诉您整数没有任何名为 Items 的属性。

    foreach (ListItem item in ListBox1.Items)
    {
        // etc
    }
    

    【讨论】:

      【解决方案3】:

      我认为您的命名约定造成了一些混乱。 您从查询字符串中获取了您的 ID,并将其命名为“lbSampleID”。 现在您正尝试使用您获得的 ID 查找 ListBox 控件,并将其声明为 int,然后运行 ​​foreach 循环查找项目。

      我建议将您的 ListBox 重命名为“lbSample”,将 ID 重命名为“sampleID”。 然后,您可以更清楚地了解您的意图。

      我猜你的意思是这样的:

      //Here is the ListBox Control
       <asp:ListBox ID="lbSample" runat="server" Height="99px" Width="131px">   </asp:ListBox>
      

      C#

      //Here is the ID of the item you want update
      string sampleID = Request.QueryString["si"].ToString();
      using (SqlCommand sc = new SqlCommand(@"Update Sample
           set ConsentConfirmed='NO' , DateConsent='',  ConsentNameID=@NewConsentName
      
              WHERE  MBID = @MBID and ConsentNameID = @ConsentID and SampleID=@SampleID", con))
                  {
                      sc.Parameters.Add("@MBID", SqlDbType.Int).Value=MBID;
      
                      sc.Parameters.Add("@ConsentID", SqlDbType.Int).Value = ConsentID;
                      sc.Parameters.Add("@NewConsentName", SqlDbType.Int).Value = NewConsentName;
      
                      int records = sc.ExecuteNonQuery();
                      int value = records;
              //lbSample is the ListBox Control containing the items
                    foreach (ListItem item in lbSample.Items)
                      {
                           //here is looking for each item that hold same value as your same ID.
                          if (item.SelectedValue == sampleID)
                          {
                              try
                              {
                                  sc.Parameters["@SampleID"].Value = item.Text;
                                  sc.ExecuteNonQuery();
                              }
      
                              catch (Exception ex)
                              {
                                  // Label1.Text = ex.Message;
                              }
      
                          }
      
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 2015-01-04
        • 2019-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多