在添加/更新列表框中的项目时出现错误 [英] getting an error in adding/updating items in listbox

查看:54
本文介绍了在添加/更新列表框中的项目时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#制作桌面应用程序.我创建了一个表单,其中有各种带有各自文本框的标签,例如阈值ID,名称,日期等)和一个列表框.我已使用2个按钮添加,更新表单.我已使用了组框,其中有一个带有2个按钮的列表框add.delete.对于整个表单,它添加/更新列表框的内容(如id,名称,日期和项(对于列表框中的每个条目项有所不同).现在,包含列表框的组框,对阈值id,名称,日期等所做的每个条目都会相应地执行添加/更新按钮问题是当我添加表单的全部内容时,出现错误

无法转换对象以键入"System.Data.DataRowView"以键入"System.String".
我检查了何时不包括列表框编码,没有错误,当我包括列表框编码时,出现错误
下面是编码

I am making desktop application using C#.I have created a form in which there are various labels with respective textboxes,combox(like threshold id,name,date etc) and one listbox.I have taken 2 buttons add ,update for a form.I have taken group box in which there is a list box with 2 buttons add.delete.For a whole form it add/update the contents(like id,name,date and items of listbox(for each entry items in listbox vary).Now group box containing listbox,add/update button operate accordingly for each entry made for threshold id,name,date etc.the problem is when i add whole contents of the form .I get an error

"Unable to cast object to type ''System.Data.DataRowView'' to type ''System.String''.
I have checked when i dont include listbox coding ,there is no error and when i include listbox coding i get an error
below is the coding

 private void btmadd_Click(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");
          con.Open();
          SqlCommand cmd = new SqlCommand("INSERT INTO" +
           " Publicity_Threshold(Threshold_id,Threshold_Name,Media_method_ID,Media_Used,Starting_Date,Ending_Date,Duration_Tenure,State,City,Area,Target_Audience,Target_Audience_Volume,Walkins_Volume,Person_Incharge,Budget,Quantum_of_Threshold,Resources)" +
            " VALUES" +
           " (@Threshold_id,@Threshold_Name,@Media_method_ID,@Media_Used,@Starting_Date,@Ending_Date,@Duration_Tenure,@State,@City,@Area,@Target_Audience,@Target_Audience_Volume,@Walkins_Volume,@Person_Incharge,@Budget,@Quantum_of_Threshold,@Resources)", con);
          cmd.Parameters.Add(new SqlParameter("@Threshold_id", Convert.ToInt32(txtThresholdID.Text)));
          cmd.Parameters.Add(new SqlParameter("@Threshold_Name", cboThreholdname.Text));
          cmd.Parameters.Add(new SqlParameter("@Media_method_ID", Convert.ToInt32(cbomethodid.Text)));
          cmd.Parameters.Add(new SqlParameter("@Media_Used", cbomediaused.Text));
          cmd.Parameters.Add(new SqlParameter("@Starting_Date", this.startingdate.Value));
          cmd.Parameters.Add(new SqlParameter("@Ending_Date", this.endingdate.Value));
          cmd.Parameters.Add(new SqlParameter("@Duration_Tenure", txtduration.Text));
          cmd.Parameters.Add(new SqlParameter("@State", cboState.Text));
          cmd.Parameters.Add(new SqlParameter("@City", cboCity.Text));
          cmd.Parameters.Add(new SqlParameter("@Area", cboArea.Text));
          cmd.Parameters.Add(new SqlParameter("@Target_Audience", cboTargetAudience.Text));
          cmd.Parameters.Add(new SqlParameter("@Target_Audience_Volume", Convert.ToInt32(txtaudiencevolume.Text)));
          cmd.Parameters.Add(new SqlParameter("@Walkins_Volume", Convert.ToInt32(txtinquiryvolume.Text)));
          cmd.Parameters.Add(new SqlParameter("@Person_Incharge", txtperson.Text));
          cmd.Parameters.Add(new SqlParameter("@Budget", Convert.ToInt32(txtBudget.Text)));
          cmd.Parameters.Add(new SqlParameter("@Quantum_of_Threshold", Convert.ToInt32(txtQuantum.Text)));
          
              string strC = "";
              foreach (string itm in listBox1.Items)
              {
                  strC = strC + " ," + itm;

              }
              cmd.Parameters.Add(new SqlParameter("@Resources",strC));        
          cmd.ExecuteNonQuery();
          MessageBox.Show("Insertion successfully done");

}

推荐答案

按照以下方式尝试


try this in the Following way


 foreach (ListItem item in ListBox1.Items)
           {
               strc += item + ",";
           }


//You are taking string for ListBox items instead of list Item


像这样对您有用

Do it like that it should work for you

listBox1.Items.Cast<object>().Aggregate((item1, item2) => item1 + "," + item2).ToString();
</object>



在您的代码中,问题出在这里



In your code the problem is here

foreach (string itm in listBox1.Items)


这篇关于在添加/更新列表框中的项目时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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