Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
            SqlCommand cmd = new SqlCommand("insert into ward values((select (patient_id) from patient),'" + "','1'" + System.DateTime.Today.Date.ToString()+ "','admit'"+ "','paid')", sc);
           
            cmd.Connection = sc;
            sc.Open();
            cmd.ExecuteNonQuery();
            sc.Close();


error: Error converting data type nvarchar to numeric.


help me to solve this error
Posted
Updated 2-Apr-14 18:54pm
v2
Comments
[no name] 2-Apr-14 12:46pm    
show your ward table column names with data type

Probably, you have an ID column, that is identity and you are not specifying it -and you can't- but because you don't specify which columns you want to load data into SQL tries to set them in internal column order.
Name your columns!
SQL
INSERT INTO MyTable (col1, col2) VALUES (1, 2)
and your problem will probably go away...
 
Share this answer
 
Comments
Maciej Los 2-Apr-14 13:10pm    
Sure!
+5!
You should know that using queries in code behind is a bad practice, especially when you're ASP.NET programmer.

How many times we have warned members about SQL injection[^]? I don't know. Please, follow the link and learn best practices.
 
Share this answer
 
Comments
Member 10694442 2-Apr-14 13:59pm    
excuse me mr.Maciej Los. i m beginner of asp.net
thats why i have such problems
Maciej Los 2-Apr-14 14:02pm    
Do not excuse. We are here to help you ;)
Maciej Los 2-Apr-14 14:03pm    
By The Way: If my answer was helpful, please mark it as a solution (green button).
PJ003 3-Apr-14 2:15am    
+5
Maciej Los 3-Apr-14 3:21am    
Thank you ;)
Hi,
This exception occures when try to insert characters in a column having numeric datatype
Happy Coding!
:)
 
Share this answer
 
refer below link you will get an idea about insert.
http://www.w3schools.com/sql/sql_insert.asp[^]

You need mention column name if you want to insert some columns into a table like

if a table has 3 cols like col1,col2.col3.inorder to insert col2, col3 values use below query

insert into tablename(col1,col2) values ('val2;,'val3');
 
Share this answer
 
i think there a mistake in insert into- select syntax


insert into ward(//specify columns) values(select patient_id ,'" + "','1'" + System.DateTime.Today.Date.ToString()+ "','admit'"+ "','paid')" from patient)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900