简体   繁体   English

System.Data.SqlClient.SqlException 转换

[英]System.Data.SqlClient.SqlException Convert

System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value 'System.Data.DataRowView' to data type int. System.Data.SqlClient.SqlException:将 varchar 值“System.Data.DataRowView”转换为数据类型 int 时转换失败。

How to convert?如何转换?

public partial class FrmItems : MaterialSkin.Controls.MaterialForm
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\Users\Admin\source\repos\Elektrokalkulace\Sklad.mdf;Integrated Security=True;Connect Timeout=30");
    SqlDataAdapter dt;
    DataTable dtCategories = new DataTable();
    DataTable dtSubCategories = new DataTable();
    DataTable dtItems = new DataTable();

    public FrmItems()
    {
        InitializeComponent();

        dt = new SqlDataAdapter("SELECT * FROM Categories", conn);
        dt.Fill(dtCategories);
        CbxCat.DataSource = dtCategories;
        CbxCat.DisplayMember = "NameCat";
        CbxCat.ValueMember = "CatId";

        var skinManager = MaterialSkinManager.Instance;
        skinManager.AddFormToManage(this);
        skinManager.Theme = MaterialSkinManager.Themes.DARK;
        skinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);

    }
    private void FrmItems_Load(object sender, EventArgs e)
    {
        // TODO: Tento řádek načte data do tabulky 'skladDataSet.Items'. Můžete jej přesunout nebo jej odstranit podle potřeby.
        this.itemsTableAdapter.Fill(this.skladDataSet.Items);
    } 

    private void CbxCat_SelectedIndexChanged(object sender, EventArgs e)
    {
        dtSubCategories.Clear();
        dt = new SqlDataAdapter("SELECT * FROM Subcategories WHERE CatId='"+ CbxCat.SelectedValue +"'", conn);
        dt.Fill(dtSubCategories);
        CbxSubcat.DataSource = dtSubCategories;
        CbxSubcat.DisplayMember = "NameSubCat";
        CbxSubcat.ValueMember = "SubCatId";
    }

    private void CbxSubcat_SelectedIndexChanged(object sender, EventArgs e)
    {
        dtItems.Clear();
        dt = new SqlDataAdapter("SELECT * FROM Items WHERE SubCatId='" + CbxSubcat.SelectedValue + "'", conn);
        dt.Fill(dtItems);
        dataGridViewItem.DataSource = dtItems;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        FrmHlavniMenu menu = new FrmHlavniMenu();
        menu.Show();
        this.Hide();
    }
}

You claim it is failing on the second line below?你声称它在下面的第二行失败了?

dt = new SqlDataAdapter("SELECT * FROM Subcategories WHERE CatId='"+ CbxCat.SelectedValue +"'", conn);
dt.Fill(dtSubCategories);

If CatId is an INT then this will fail because of the apostrophes (') surrounding the value.如果 CatId 是 INT,那么这将失败,因为值周围有撇号 (')。 And please use parameters:并请使用参数:

dt = new SqlDataAdapter("SELECT * FROM Subcategories WHERE CatId = @CatId", conn);
dt.SelectCommand.Parameters.AddWithValue("@CatId", CbxCat.SelectedValue);
dt.Fill(dtSubCategories);

Secondly, what is the type of CbxCat.SelectedValue ?其次, CbxCat.SelectedValue的类型是CbxCat.SelectedValue If it returns a string then you will need to parse the value as an integer: int.Parse(CbxCat.SelectedValue)如果它返回一个字符串,那么您需要将该值解析为一个整数: int.Parse(CbxCat.SelectedValue)

But the exception suggests it is actually of type System.Data.DataRowView .但异常表明它实际上是System.Data.DataRowView类型。 In that case you need to access the the item property, for example, CbxCat.SelectedValue["CategoryId"]在这种情况下,您需要访问item属性,例如, CbxCat.SelectedValue["CategoryId"]

See for more info on DataRowView: https://docs.microsoft.com/en-us/dotnet/api/system.data.datarowview.item?view=netframework-4.8#System_Data_DataRowView_Item_System_String_有关 DataRowView 的更多信息,请参见: https ://docs.microsoft.com/en-us/dotnet/api/system.data.datarowview.item ? view = netframework-4.8#System_Data_DataRowView_Item_System_String_

I finally solved it this way.我最后是这样解决的。 It took me a lot of effort, but it was worth it.我花了很多精力,但这是值得的。 Thank you all for your support and thank you very very much.感谢大家的支持,非常感谢。

enter code here在此处输入代码

public partial class FrmItems : MaterialSkin.Controls.MaterialForm
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\Users\Admin\source\repos\Elektrokalkulace\Stock.mdf;Integrated Security=True;Connect Timeout=30");

    int CatId;

    public FrmItems()
    {
        InitializeComponent();

        refreshCat();

        var skinManager = MaterialSkinManager.Instance;
        skinManager.AddFormToManage(this);
        skinManager.Theme = MaterialSkinManager.Themes.DARK;
        skinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);

    }
    private void FrmItems_Load(object sender, EventArgs e)
    {

    }
    private void refreshCat()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Categories", conn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        conn.Close();
        CbxCat.DisplayMember = "NameCat";
        CbxCat.ValueMember = "CatId";
        CbxCat.DataSource = dt;
    }


    private void CbxCat_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CbxCat.SelectedValue.ToString() != null)
        {
            CatId = Convert.ToInt32(CbxCat.SelectedValue.ToString());

            refreshSubcat(CatId);
        }
    }

    private void refreshSubcat(int CatId)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Subcategories WHERE CatId=@CatId", conn);
        cmd.Parameters.AddWithValue("CatId", CatId);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        conn.Close();
        CbxSubcat.DisplayMember = "NameSubcat";
        CbxSubcat.ValueMember = "SubcatId";
        CbxSubcat.DataSource = dt;
    }   
    private void CbxSubcat_SelectedIndexChanged(object sender, EventArgs e)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Items WHERE SubCatId='" + CbxSubcat.SelectedValue + "'", conn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        conn.Close();
        dataGridViewItem.DataSource = dt;

    }


    private void pictureBox1_Click(object sender, EventArgs e)
    {
        FrmMainMenu menu = new FrmMainMenu();
        menu.Show();
        this.Hide();
    }
}
enter code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM