如何将combobox1.selectedvalue转换为字符串 [英] How to convert combobox1.selectedvalue to string

查看:280
本文介绍了如何将combobox1.selectedvalue转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,tbl1有主键和字符串,tbl2有那个主键的外键,当我尝试从combobox插入tbl2值。选择值

发生此错误

将nvarchar值'FMCS17002'转换为数据类型int时转换失败。



我尝试使用

I have two tables, tbl1 have primary key with string, tbl2 have foreign key to that primary key ,when I try to insert into tbl2 value from combobox.Selected value
this error was occurred
Conversion failed when converting the nvarchar value 'FMCS17002' to data type int.

I try to use

CmbStudents.SelectedValue.ToString()
but not working 
and CmbStudents.SelectedItem
also not Working
Thank's ,,,

What I have tried:

cmd = New SqlCommand("insert into SupervisionAndExaminer values(@p1, @p2, @p3)", SqlSeverDB)

            cmd.Parameters.AddWithValue("@p1", CmbStudents.SelectedValue)
            cmd.Parameters.AddWithValue("@p2", CmbInstructorss.SelectedValue)
            cmd.Parameters.AddWithValue("@p3", CmbStudents.SelectedValue)
            cmd.ExecuteNonQuery()

推荐答案

您要输入val的列ues有数据类型integer



确保根据表格列的数据类型提供数据。



如果你想添加FMCS17002,你必须设置数据类型varchar(30),其中30是一个字符的长度,你可以根据需要设置长度,我刚刚设置了例子。



你不能把这个值作为整数插入。



我希望你明白我的观点。
The column where you are entering values has datatype integer

Make sure you are supplying data according to data type of a column of a table.

If you want to add "FMCS17002" then you must have to set datatype varchar(30) where 30 is a length of a character, You can set length according to your need, I have just set for example.

You can not insert this value as an integer.

I hope you got my point.


错误是

The error is
Conversion failed when converting the nvarchar value 'FMCS17002' to data type int

所以你不要我想要一个字符串(在你的情况下已经是 SelectedValue ),但是是一个整数。您尝试设置的至少一个数据库字段的类型为 int ,字符串值FMCS17002无法转换为整数。



因此,您必须更改数据库以使用相应字段的字符串( NVARCHAR )或更改组合框以仅包含整数(或代表有效整数的字符串,以便它们可以被转换)。





发布我的解决方案后问题已被更改:

So you don't want a string (what SelectedValue already is in your case) but an integer. At least one of the database fields you are trying to set has the type int and the string value "FMCS17002" can't be converted to an integer.

So you have to change the database to use strings (NVARCHAR) for the corresponding fields or change the combo boxes to contain only integers (or strings representing valid integers so that they can be converted).


The question has been changed after posting my solution:

引用:

我有两个表,tbl1有主键和字符串,tbl2有外键到那个主表key,当我尝试从combobox.selected值插入tbl2值时

I have two tables, tbl1 have primary key with string, tbl2 have foreign key to that primary key ,when I try to insert into tbl2 value from combobox.Selected value

你必须首先使用相应的组合框字符串从tbl1查询ID:

You have to query the ID from tbl1 first using the corresponding combo box string:

SELECT id FROM tbl1 WHERE name=selected_value

并将返回的ID作为参数传递给 INSERT 命令。

[/ EDIT]

and pass the returned ID as parameter to the INSERT command.
[/EDIT]


谢谢我的朋友^^

我的代码中的错误我交换组合框的位置并重复
Thank you my friends ^^
the error in my code I exchange the positions of comboboxes and repeat
CmbStudents.SelectedValue



并且函数中有另一个错误填充我的组合框

问题解决了^^


and there is another error in function that fill my combobox
The Problem was solved ^^


这篇关于如何将combobox1.selectedvalue转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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