I want to get from the table the receipt number by the product name and the date entered in the datetimepicker :

 string data = Data.Data_ret(ref dateTimePicker1); // получаю дату из datetimepicker string tovarid = Data.QuerysScale("Select NumberOfTovar From Tovar where Name = '" + comboTovarName.Text + "'");// нахожу номер товара по имени из combobox. string check_1 = Data.QuerysScale("Select NumberOfEntrance from Entrance where NumberOfTovar = " + tovarid + " AND Data = " + data + ""); // здесь хочу получить номер поступления по номеру товара и дате. 

The code works, but the problem is that check_1 always null , although the table contains goods receipts with the date I chose. By the way, I applied this query to my database in Access, and it worked fine. What could be the problem?

The method I used to get the date:

 static public string Data_ret(ref DateTimePicker dateTimePicker1) { string[] mounth = { "января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября","декабря"}; string Data = dateTimePicker1.Text; Data = Data.Remove(Data.Length - 3); int tmp; string day, preMonth; if (int.TryParse(Data.ElementAt(1).ToString(), out tmp)) { day = Data.Substring(0, 2); preMonth = Data.Substring(2, Data.Length - 6).Trim(); } else { day = "0" + Data.ElementAt(0).ToString(); preMonth = Data.Substring(1, Data.Length - 5).Trim(); } string year = Data.Substring(Data.Length - 4); var month = string.Empty; for (int i = 0; i < mounth.Length; i++) { if (preMonth == mounth[i].Trim()) { month = "0" + (i + 1); break; } } var result = string.Concat(day, "/", month, "/", year); return result; } 

    1 answer 1

    it is not clear why the method "Date_ret" is needed; if for getting a date, then the standard property did not suit

    DateTimePicker.Value

     //myDate будет содержать введенное поле, //и его можно будет привести к любому необходимому формату DateTime myDate = DateTimePicker1.Value; 

    for a more specific answer, specify the structure of the required tables, which database. well, sample data to process

    UPD

    and if you try something like this:

     var query = String.Format("Select NumberOfEntrance from Entrance where NumberOfTovar = {0} and Data='{1}'", tovarId, dateTimePicker1.Value.ToString()); 

    But in general, the formation of a query in this way is not good, I would probably do something like this:

     //создаем SqlConnection SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "строка подключения" SqlDataReader sqlReader = null; var query = "Select NumberOfEntrance from Entrance where NumberOfTovar = @tovarId and Data=@data"; var sqlParTovarId = new SqlParameter("@tovarId", SqlDbType.Int); var sqlParData = new SqlParameter("@data", SqlDbType.DateTime); sqlParTovarId = tovarId; sqlParData = DateTimePicker1.Value; var sqlCmd = new SqlCommand(query, sqlConnection); sqlCmd.Parameters.Add(sqlParTovarId); sqlCmd.Parameters.Add(sqlParData); //так если возвращается несколько значений sqlReader = sqlCmd.ExecuteReader(); while(sqlReader.Read()) { //делаем то что необходимо } //так если одно var NumberOfEntrace = (int)sqlCmd.ExecuteScalar(); 

    But in general, consider the possibility of using ORM (I use the Entity Framework) and linq. in small projects I think its use is more justified than writing requests manually. See the Entity Framework Guide here for your reference.

    • tried using datetimepicker.value now in string string check_1 = Data.QuerysScale ("Select NumberOfEntrance from Entrance where NumberOfTovar =" + tovarid + "AND Data =" + dateTimePicker1.Value.ToString () + "")); syntax error, missing operator. - Roman1
    • What type is the "Date" field in the "Entrace" table? - Bald
    • date / time and field is called Data - Roman1
    • @ Roman1 updated the answer - Bald