I have a script which is running perfectly in local host. the code is this
string sql = "insert into Usertable ";
sql += "values(" + mVendid + ", '" + usrname + "','" + usrpass + "', CONVERT(datetime, " + datecreation + ", 103)" + "," + createdby + ")";
The values are (1,'sa','ee','05/18/2013', 1)
This is also running fine if I run the same in sql server. But I am having only problem when I am inserting the date field. If I remove teh date field in insert statement the code is running absolutely fine
But while running the same in webserver it is encountering an error as
Server Error in '/' Application.
Divide by zero error encountered.
The statement has been terminated.
Using code 103 with CONVERT means that you're suppling as input a date in the format dd/mm/yy while in your code you are using mm/dd/yy format.
You could try with these values:
(1,'sa','ee','18/05/2013', 1)
For further reference about convert function check this out
Related
Why the value stored in the database is the username of the SQL Server instead of the username of the system user? How to get the name of current system user?
string Query = "insert into Locate(Locate_LongLatOut, Locate_IC, CreateDate, CreateBy) values('" + this.txtCoordinate.Text + "', '" + this.txtCustomerIC.Text + "', getdate(), USER_NAME()); ";
Use CURRENT_USER instead USER_NAME().
i.e:
SELECT CURRENT_USER;
I have a simple gridview which reads data from my SQL server database, and shows it. The data is partly in Hebrew. But when I use the gidview to read the data - I get question marks instead of Hebrew letters... once i checked my database table it also stored as question marks.
What Should I do ? changing the settings of my SQL server or should I change my SQL command statement
Please need help.
I am using Vb 2008 Windows Application C# and SQL server 2014
Code
if (!string.IsNullOrEmpty(txt_C_Request.Text))
{
con = new SqlConnection("Server=" + server + ";Database=" + db + ";User Id=" + user + ";Password=" + pass + "");
SqlCommand Insert_Temp = new SqlCommand("insert into [QAMNI].[dbo].[tbl_Talab_Temp] ([T_ID],[Items_ID],[Car_ID],[Items_Name]) values ('" + txt_T_ID.Text + "','" + txtTempCount.Text + "','" + txt_Car_ID.Text + "','" + txt_C_Request.Text + "')", con);
con.Open();
Insert_Temp.ExecuteNonQuery();
con.Close();
txt_C_Request.Text = "";
Get_Count_Insert_Temp();
Get_Tem_GV();
txt_C_Request.Focus();
}
Thank you
You need to use a data type nvarchar or nchar in order to store Unicode characters. Unicode character literals must also be prefixed with N.
So, instead of
set address = 'כַּף סוֹפִית'
Write to a nvarchar field without the unicode prefix
set address = N'כַּף סוֹפִית'
Take a look at this: N prefix before string in Transact-SQL query
I need to insert new record into a SQL Server database, but get
Incorrect syntax error
The strange thing is when I try to query the same statement in SQL Server itself, it works properly.
The code in vb.net is as follows:
insertSql = "INSERT INTO Seg_LINE VALUES (" & OBJECTID & ", 'test" + "', '" + "test" + "','" + DrainName + "'," & UID & ")"
logger.Info("insert sql = " + insertSql)
Dim cmdInsert As New SqlClient.SqlCommand(insertSql, Sqlconnection)
cmdInsert.ExecuteNonQuery()
The OBJECTID and UID are number parameters.
I cannot figure out what's wrong with my code, I am using vb.net(vs2102).
Most likely you have a DrainName value with a single quote in it. You're lucky the query is just failing, and not executing unwanted commands on your DB server. Don't use string concatenation like that to build queries! You need to use query parameters, like this:
insertSql = "INSERT INTO Seg_LINE VALUES (#ObjectID, 'test', 'test', #DrainName, #UID)"
logger.Info("insert sql = " + insertSql)
Dim cmdInsert As New SqlClient.SqlCommand(insertSql, Sqlconnection)
'I'm guessing at these parameter types. Use the actual db types of the columns
cmdInsert.Parameters.Add("#ObjectID", SqlDbType.Int).Value = OBJECTID
cmdInsert.Parameters.Add("#DrainName", SqlDbType.NChar, 50).Value = DrainName
cmdInsert.Parameters.Add("#UID", SqlDbType.Int).Value = UID
cmdInsert.ExecuteNonQuery()
Changing the code this way will also likely fix your syntax error.
I am rewriting more code from Access to SQL Server and I have never encountered a TRIM code before, any ideas how to write it for SQL Server as far as I know there is not just plain TRIM for SQL Server
Trim([FCT_TYP_CD]) & " (" & Trim([Dep_TYP_CD]) & ")" AS [Course Owner]
SELECT RTRIM(LTRIM([FCT_TYP_CD])) + ' (' +
RTRIM(LTRIM([Dep_TYP_CD])) + ')' AS [Course Owner]
LTRIM(#String)
RTRIM(#string)
or
LTRIM(RTRIM(#string))
In order to automate some actions in MSI I have been trying to insert into RemoveFile table using JScript and keep getting errors and since there is not much error description i am not able to figure out the issue in query and the only error that i get when i try to debug using csript is -2147467259 OpenView,SQL,
This is the query that i am using to insert into removefile table, Can anyone please help me figuring out the issue.
"INSERT INTO `RemoveFile` (`FileKey`, `Component_`, `FileName`, `DirProperty`, `InstallMode`) VALUES (`_142D31F52C744D6FB945F01BA06EEFB3`, `C__931358B017AE83C769F5CB9E95BD2401`, `Product version 2.0.lnk`, `DesktopFolder`, 1)
Have you opened the view on the installer database?
var installer = WScript.CreateObject("WindowsInstaller.Installer");
var database = installer.OpenDatabase(<your-product.msi>,
msiOpenDatabaseModeTransact);
sql = "INSERT INTO `RemoveFile` "
+ "(`FileKey`, `Component_`, `FileName`, "
+ "`DirProperty`, `InstallMode`) "
+ "VALUES ('_142D31F52C744D6FB945F01BA06EEFB3', "
+ "'C__931358B017AE83C769F5CB9E95BD2401', "
+ "'Product version 2.0.lnk', 'DesktopFolder', 1)";
view = database.OpenView(sql);
view.Execute();
view.Close();