Error Message on Console.Writeline and date converting.

  • Thread starter Thread starter F.oliveirarocha
  • Start date Start date
F

F.oliveirarocha

Guest
Hi folks I'm still learning this language and sometimes it gets frustrating.

I'm starting a project where where it will have repositories. And I'm first making the CRUD to see all the procedures working.

I'm getting an error message when the Insert procedure execute the line 47, "ExecuteNonQuery".

The DateTime converstion seems to be not working, once that the error message mentions it.

Here below follows the code and the table as well.

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConexaoBD
{
class Program
{
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection(@"data source=DESKTOP-3O98051; Integrated Security= SSPI; Initial Catalog= AutocarWeb");
connection.Open();

// string strQueryUpdate = "UPDATE Users SET username = 'Francisco' WHERE userID = 1 ";
// SqlCommand commandUpdate = new SqlCommand(strQueryUpdate, connection);
// commandUpdate.ExecuteNonQuery();

// string strQueryDelete = "DELETE FROM Users WHERE userID = 13 ";
// SqlCommand commandDelete = new SqlCommand(strQueryDelete, connection);
// commandDelete.ExecuteNonQuery();

Console.Write("Digite o Nome: ");
string nome = Console.ReadLine();

Console.Write("Digite o Login: ");
string login = Console.ReadLine();

Console.Write("Digite a Senha: ");
string senha = Console.ReadLine();

Console.Write("Digite a Data: ");
string data1 = Console.ReadLine();
DateTime data = Convert.ToDateTime(data1);

Console.Write("Digite o NomeLogado: ");
string nomelogado = Console.ReadLine();

Console.Write("Digite a Data de Hoje: ");
string datalogado1 = Console.ReadLine();
DateTime datalogado = Convert.ToDateTime(datalogado1);

string strQueryInsert = string.Format("INSERT INTO Users(username, loginname, password, date, userlogged, datelogged) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", nome, login, senha, data, nomelogado, datalogado);
SqlCommand commandInsert = new SqlCommand(strQueryInsert, connection);
commandInsert.ExecuteNonQuery();


string strQuerySelect = "SELECT * from Users";
SqlCommand commandSelect = new SqlCommand(strQuerySelect, connection);
SqlDataReader reader = commandSelect.ExecuteReader();

while (reader.Read())
{
Console.WriteLine("ID:{0}, Nome:{1}, Login:{2}, Senha:{3}, Data:{4}, UserLogado:{5}, DataLogado:{6}", reader["userID"], reader["username"], reader["loginname"], reader["password"], reader["date"], reader["userlogged"], reader["datelogged"]);
}
}
}
}


The Table - SQLServer 2014

userID int Unchecked
username varchar(50) Checked
loginname varchar(25) Checked
password varchar(25) Checked
date datetime Checked
userlogged varchar(25) Checked
datelogged datetime Checked


Error Message:

Exceção Sem Tratamento: System.Data.SqlClient.SqlException: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
em System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
em System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
em System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
em System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
em System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
em System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
em System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
em ConexaoBD.Program.Main(String[] args) na D:\AUTOCARWEB\SolutionAutocarWeb\ConexaoBD\Program.cs:linha 47

Can someone help me with that? Why the convertion of datetime is not working ?

Thanks for all of you.

Flávio Rocha.

Continue reading...
 
Back
Top