Watch this on YouTube
Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the varchar value 'Select * from Cities where ID = ' to data type int.
to fix it replace
Declare @ID int = 1
Declare @SQL varchar(max)
Set @SQL = 'Select * from Cities where ID = ' + @ID
exec (@SQL)
with
Declare @ID int = 1
Declare @SQL varchar(max)
Set @SQL = 'Select * from Cities where ID = ' + CAST( @ID AS VARCHAR)
exec (@SQL)
Hello, please i need help, i have similar error how can i solve it? "conversion failed when converting the varchar value 2020-5-22 to data type int"
ReplyDeletemore details about my project and error
ReplyDeleteusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace NSPM_Sales_Invoice
{
public partial class ViewBills : Form
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.NSPM_Sales_InvoiceCon);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
public ViewBills()
{
InitializeComponent();
}
private void ViewBills_Load(object sender, EventArgs e)
{
con.Open();
da = new SqlDataAdapter("Select * from Tbl_HeaderData order by JobNo asc", con);
con.Close();
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void btnViewBills_Click(object sender, EventArgs e)
{
con.Open();
da = new SqlDataAdapter("Select * from Tbl_HeaderData where JobNo between '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "'and'" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "'order by JobNo asc ", con);
DataSet ds = new DataSet();
da.Fill(ds, "Tbl_HeaderData"); //The error i wrote above was shown on this line.
dataGridView1.DataSource = ds.Tables["Tbl_HeaderData"];
con.Close();
}
}
}