Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从组合框到文本框_C#_Sql_Datetime_Datetimepicker - Fatal编程技术网

C# 从组合框到文本框

C# 从组合框到文本框,c#,sql,datetime,datetimepicker,C#,Sql,Datetime,Datetimepicker,我不知道我的代码出了什么问题,或者解决方案可能会给我带来什么。它只是告诉我,当我不使用任何只使用int的字符串时,它无法从string转换为int 我也不知道如何编写并将datetime放回datetimepicker private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e) { string constring = "Data Source = (LocalDB)\

我不知道我的代码出了什么问题,或者解决方案可能会给我带来什么。它只是告诉我,当我不使用任何只使用int的字符串时,它无法从string转换为int 我也不知道如何编写并将datetime放回datetimepicker

private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
            string Query = "SELECT * FROM Products where Name='" + cbxProducts.Text + "' ; ";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
            SqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                string sBarcode = myReader.GetString("Barcodes");
                string sName = myReader.GetString("Name");
                var sDate = myReader.GetDateTime("EDate");
                string sQuantity = myReader.GetInt32("Quantity")).ToString();
                string sPrice = myReader.GetInt32("Price")).ToString();
                tbxBar.Text = sBarcode;
                tbxName.Text = sName;
                sDate = dateDate.Value;
                tbxPrice.Text = sPrice;
                tbxQua.Text = sQuantity;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
编辑:

编辑2:我收到错误消息

“当没有可用数据时,尝试读取无效”


我的数据库中的所有字符串中都有数据,但仍然出现此错误

您必须使用列的索引,而不是名称

string sBarcode = myReader.GetString(IndexOfBarcodesColumn);
像这样的

string sBarcode = myReader.GetString(0);
string sName = myReader.GetString(1);
var sDate = myReader.GetDateTime(2);
string sQuantity = myReader.GetInt32(3).ToString();
string sPrice = myReader.GetInt32(4).ToString();
或者您可以直接从读卡器使用字段名

string sBarcode = myReader.Item["Barcodes"];
string sName = myReader.Item["Name"];
var sDate = myReader.Item["EDate"];
string sQuantity = myReader.Item["Quantity"];
string sPrice = myReader.Item["Price"];

您必须使用列的索引,而不是名称

string sBarcode = myReader.GetString(IndexOfBarcodesColumn);
像这样的

string sBarcode = myReader.GetString(0);
string sName = myReader.GetString(1);
var sDate = myReader.GetDateTime(2);
string sQuantity = myReader.GetInt32(3).ToString();
string sPrice = myReader.GetInt32(4).ToString();
或者您可以直接从读卡器使用字段名

string sBarcode = myReader.Item["Barcodes"];
string sName = myReader.Item["Name"];
var sDate = myReader.Item["EDate"];
string sQuantity = myReader.Item["Quantity"];
string sPrice = myReader.Item["Price"];

像这样,它应该与columnnames一起工作,或者如果您想使用columnIndex,请按照@MtwStark在其回答中描述的方式进行

string sBarcode = myReader["Barcodes"].ToString();
string sName = myReader["Name"].ToString();
var sDate = myReader["EDate"];
string sQuantity = myReader["Quantity"].ToString();
string sPrice = myReader["Price"].ToString();

关于以下错误消息

“当没有可用数据时,尝试读取无效”

您必须先调用
myReader.Read()
。(就像有人进来问的那样)


像这样,它应该与columnnames一起工作,或者如果您想使用columnIndex,请按照@MtwStark在其回答中描述的方式进行

string sBarcode = myReader["Barcodes"].ToString();
string sName = myReader["Name"].ToString();
var sDate = myReader["EDate"];
string sQuantity = myReader["Quantity"].ToString();
string sPrice = myReader["Price"].ToString();

关于以下错误消息

“当没有可用数据时,尝试读取无效”

您必须先调用
myReader.Read()
。(就像有人进来问的那样)



你能提供准确的错误信息吗?我认为要么
stringsquantity=myReader.GetInt32(“数量”).ToString()
字符串sPrice=myReader.GetInt32(“价格”).ToString()是错误的。检查数据库中的数据类型。根据这些信息,我们只能猜测。我猜价格或数量或两者都不是int。当我悬停时,无法从字符串转换为int,但我无法运行代码,因为“条形码”,并且所有条形码都用红色下划线。您应该始终使用。这种类型的字符串连接对攻击是开放的。还可以使用
使用
语句来自动处理连接和命令。您的问题是为什么会出现“无法从字符串转换为int”错误,答案是因为
GetString
GetDateTime
GetInt32
方法使用字段索引而不是字段名。接受答案并尝试自己解决程序的问题,或提出一个关于按名称获取字段值的新问题(此处也已回答),请提供确切的错误消息?我认为要么
stringsquantity=myReader.GetInt32(“数量”).ToString()
字符串sPrice=myReader.GetInt32(“价格”).ToString()是错误的。检查数据库中的数据类型。根据这些信息,我们只能猜测。我猜价格或数量或两者都不是int。当我悬停时,无法从字符串转换为int,但我无法运行代码,因为“条形码”,并且所有条形码都用红色下划线。您应该始终使用。这种类型的字符串连接对攻击是开放的。还可以使用
使用
语句来自动处理连接和命令。您的问题是为什么会出现“无法从字符串转换为int”错误,答案是因为
GetString
GetDateTime
GetInt32
方法使用字段索引而不是字段名。接受一个答案,试着自己找出程序的解决方案,或者提出一个关于按名称获取字段值的新问题(此处也已回答)好,我对数据库不熟悉,所以如何找到不同列的索引?如果使用
SELECT*FROM Products
列的顺序是在数据库中创建的顺序,但可以使用
SELECT barcode、Name、EDate、Quantity、,产品价格
因此条形码将有索引0、名称索引1等。如果这回答了您的问题,请接受answer@MtwStark我认为我们都是对的和错的。看一看。你的语法似乎是VB.NET和C#的混合体。现在你是对的,我用VB表示法代替C#方括号。我现在已经改正了!:)我使用vb,这是一个ctrl-c/ctrl-v错误;)好的,我对数据库不太熟悉,那么如何找到不同列的索引呢?如果您使用
SELECT*FROM Products
columns的顺序是在数据库中创建的顺序,但是您可以使用
SELECT barcode、Name、EDate、Quantity、,产品价格
因此条形码将有索引0、名称索引1等。如果这回答了您的问题,请接受answer@MtwStark我认为我们都是对的和错的。看一看。你的语法似乎是VB.NET和C#的混合体。现在你是对的,我用VB表示法代替C#方括号。我现在已经改正了!:)我使用vb,这是一个ctrl-c/ctrl-v错误;)当我执行此操作时,myReader get的下划线为read,并显示“预期的方法名称”修复了它,以便我可以运行代码,但当我尝试从组合框中进行选择时,出现了错误“C#在没有可用数据时尝试读取无效”“如果您原来的问题得到了回答,您应该在此处接受答案并创建一个新问题。@MtwStark Yes,应该有效。”。这同样适用于VB.NET。我从不写
myReader.Item(“FieldName”)
我总是使用
myReader(“FieldName”)
@HannesCorbett问题不是“请让我的程序工作”,而是“为什么我会出现这个错误?”所以当我这样做时,答案是myReader会被加下划线,它说“需要方法名”修复了它,这样我就可以运行代码了,但我得到了错误“C#在没有可用数据的情况下,当我尝试从我的组合框中进行选择时”如果您的原始问题为