解析错误消息无法将对象强制转换为类型'System.String'以键入'System.Data.DataRowView' [英] resolving error message Unable to cast object to type 'System.String' to type 'System.Data.DataRowView'

查看:116
本文介绍了解析错误消息无法将对象强制转换为类型'System.String'以键入'System.Data.DataRowView'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,系统管理员使用保存按钮将数据输入到tblministries表(例如A B C D E F)。当表单加载时,A B C D E F加载到checkedlistbox(cblMinistries)。用户可以检查checkedlistbox中的任何数据(例如B C F),然后使用用户成员ID将其保存到tblMembershipministry表中。当成员输入其ID并单击搜索按钮时,表单应将A B C D E F加载到checkedlistbox中,并且应在checklistbox中检查来自tblMembershipministry表(B C F)的具有相同成员ID的任何数据。我的问题是我不断收到错误无法将对象强制转换为'System.String'以在此代码行上键入'System.Data.DataRowView'DataRowView row =(DataRowView)this.cblMinistries.Items [i] ;请帮帮我



代码填写

  public   void  PopuMinistry()
{
SqlConnection conn = new SqlConnection( 数据源= USER-PC;初始目录= PIWCDB;用户ID = sa;密码=迈克 );
if (conn.State!= ConnectionState.Open)
{
conn.Open();
}
string SqlDataPull =( 选择Homecellname从tblministries按Homecellname排序);

SqlCommand cmd = new SqlCommand(SqlDataPull);
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();


while (dr.Read())
{
SqlDataPull = dr [ 0 ]的ToString();
cblMinistries.Items.Add(SqlDataPull);
}
dr.Close();
}



用于将checklistbox项目标记为已检查的代码

  public   void  minsearch()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings [ ConnectionString]);
conn.ConnectionString = 数据源= USER-PC;初始目录= PIWCDB;用户ID = sa;密码=迈克;
conn.Open();

string sqlQuery = SELECT Ministryname FROM tblMembershipministry WHERE FormattedMembershipid =' + this .txtMembershipid.Text + < span class =code-string>'
;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sqlQuery;
cmd.CommandType = System.Data.CommandType.Text;

try
{
System.Data.SqlClient.SqlDataReader dr = ;
dr = cmd.ExecuteReader();

while (dr.Read())
{
for int i = 0 ; i < ; cblMinistries.Items.Count; i ++)
{
DataRowView row =(DataRowView) this .cblMinistries.Items [i] ;

if (row [cblMinistries.ValueMember] .ToString()== dr [ Ministryname]。ToString())
{
cblMinistries.SetItemChecked(i,);
}
}
}

dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

解决方案

更改

 DataRowView row =(DataRowView) this  .cblMinistries.Items [i]; 

if (row [cblMinistries.ValueMember] .ToString()== dr [ Ministryname]。ToString())
{
cblMinistries.SetItemChecked(i,);
}



to

  if  this  .cblMinistries.Items [i] .Value == dr [  Ministryname]。ToString())
{
cblMinistries.SetItemChecked(i, true );
}





您可以使用DataTable或DataSet转换为DataRowView。在这里你将字符串项添加到cblMinistries


正如错误所示,你正在将一种类型转换为另一种类型,

在转换为字符串之前访问datarow列,例如: (字符串)数据行[0]

I have a form where the system administrator enters data into a tblministries table (e.g. A B C D E F) using save button. when the form loads, A B C D E F loads into the checkedlistbox (cblMinistries). a user can check any of the data in the checkedlistbox (e.g. B C F) Which is then saved into the tblMembershipministry table with the users member ID. When a member enter his ID and click the search button, the form should load A B C D E F into the checkedlistbox and any data with the same member ID from the tblMembershipministry table (B C F) should be checked in the checklistbox. My problem is i keep getting the error "Unable to cast object to type 'System.String' to type 'System.Data.DataRowView'" on this line of code "DataRowView row = (DataRowView)this.cblMinistries.Items[i];" please help me out

code for populating the

public void PopuMinistry()
{
    SqlConnection conn = new SqlConnection("Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike");
    if (conn.State != ConnectionState.Open)
    {
        conn.Open();
    }
    string SqlDataPull = ("select Homecellname from tblministries order by Homecellname");

    SqlCommand cmd = new SqlCommand(SqlDataPull);
    cmd.Connection = conn;
    cmd.CommandType = CommandType.Text;
    SqlDataReader dr = cmd.ExecuteReader();
    

    while (dr.Read())
    {
        SqlDataPull = dr[0].ToString();
        cblMinistries.Items.Add(SqlDataPull);
    }
    dr.Close();
}


code for marking checklistbox items as checked

public void minsearch()
{
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike";
    conn.Open();

    string sqlQuery = "SELECT Ministryname FROM tblMembershipministry WHERE FormattedMembershipid = '" + this.txtMembershipid.Text + "'";
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = sqlQuery;
    cmd.CommandType = System.Data.CommandType.Text;

    try
    {
        System.Data.SqlClient.SqlDataReader dr = null;
        dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            for (int i = 0; i < cblMinistries.Items.Count; i++)
            {
                DataRowView row = (DataRowView)this.cblMinistries.Items[i];
               
                if (row[cblMinistries.ValueMember].ToString() == dr["Ministryname"].ToString())
                {
                    cblMinistries.SetItemChecked(i, true);
                }                     
            }
        }
        
        dr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

解决方案

change

DataRowView row = (DataRowView)this.cblMinistries.Items[i];
               
if (row[cblMinistries.ValueMember].ToString() == dr["Ministryname"].ToString())
{
    cblMinistries.SetItemChecked(i, true);
}  


to

if (this.cblMinistries.Items[i].Value == dr["Ministryname"].ToString())
{
    cblMinistries.SetItemChecked(i, true);
}  



you can cast to DataRowView you binding with DataTable or DataSet. here you adding string items to cblMinistries


As the error suggests, you are converting one type to another,
Access the datarow column before converting to string for e.g. (string)dataRow[0].


这篇关于解析错误消息无法将对象强制转换为类型'System.String'以键入'System.Data.DataRowView'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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