How to insert decimal value from textbox into database with c#How can I store SQL Server query value to variable?How do I calculate someone's age in C#?How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to return only the Date from a SQL Server DateTime datatypeHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#How to loop through all enum values in C#?How do I UPDATE from a SELECT in SQL Server?Adding textbox values to an SQL database in c#

Why is the saxophone not common in classical repertoire?

What does "another" mean in this case?

Recolour existing plots

My players like to search everything. What do they find?

Should I cheat if the majority does it?

Why would a propellor have blades of different lengths?

Did Snape really give Umbridge a fake Veritaserum potion that Harry later pretended to drink?

Phrasing "it says" or "it reads"

Language Selector

Where is read command?

Auto replacement of characters

If a creature is blocking and it has vigilance does it still tap?

Term for a character that only exists to be talked to

How long had Bertha Mason been in the attic at the point of the events in Jane Eyre

Does this circuit have marginal voltage level problem?

Why is quantum gravity non-renormalizable?

Cannot update a field to a Lookup, MasterDetail, or Hierarchy from something else (44:13)

List of Implementations for common OR problems

A grammar issue?

Contributing to a candidate as a Foreign National US Resident?

Upload csv into QGIS

Is よう an adjective or a noun?

In National Velvet why didn't they use a stunt double for Elizabeth Taylor?

What is meaning of 4 letter acronyms in Roman names like Titus Flavius T. f. T. n. Sabinus?



How to insert decimal value from textbox into database with c#


How can I store SQL Server query value to variable?How do I calculate someone's age in C#?How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to return only the Date from a SQL Server DateTime datatypeHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#How to loop through all enum values in C#?How do I UPDATE from a SELECT in SQL Server?Adding textbox values to an SQL database in c#






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm using a SQL Server database and Windows Forms application. I have 2 textboxes and 1 combobox. Users inputs their name and working rate into the textboxes and they chose year from combobox. I try to insert those values into my database.



EX:



2018 Mike 39,72 WORKERS


In database:



year --> nvarchar(4)
name --> nvarchar(50)
rate --> decimal(18,2)
type --> nvarchar(50)


My code is like:



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES ('" + yearcombo.Text + "','" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')", connection);


I get this error:




There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.











share|improve this question
























  • I added an example of how the insert would be vulnerable to my answer, please have a look.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:23











  • WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

    – marc_s
    Mar 20 at 12:38

















2















I'm using a SQL Server database and Windows Forms application. I have 2 textboxes and 1 combobox. Users inputs their name and working rate into the textboxes and they chose year from combobox. I try to insert those values into my database.



EX:



2018 Mike 39,72 WORKERS


In database:



year --> nvarchar(4)
name --> nvarchar(50)
rate --> decimal(18,2)
type --> nvarchar(50)


My code is like:



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES ('" + yearcombo.Text + "','" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')", connection);


I get this error:




There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.











share|improve this question
























  • I added an example of how the insert would be vulnerable to my answer, please have a look.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:23











  • WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

    – marc_s
    Mar 20 at 12:38













2












2








2


1






I'm using a SQL Server database and Windows Forms application. I have 2 textboxes and 1 combobox. Users inputs their name and working rate into the textboxes and they chose year from combobox. I try to insert those values into my database.



EX:



2018 Mike 39,72 WORKERS


In database:



year --> nvarchar(4)
name --> nvarchar(50)
rate --> decimal(18,2)
type --> nvarchar(50)


My code is like:



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES ('" + yearcombo.Text + "','" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')", connection);


I get this error:




There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.











share|improve this question
















I'm using a SQL Server database and Windows Forms application. I have 2 textboxes and 1 combobox. Users inputs their name and working rate into the textboxes and they chose year from combobox. I try to insert those values into my database.



EX:



2018 Mike 39,72 WORKERS


In database:



year --> nvarchar(4)
name --> nvarchar(50)
rate --> decimal(18,2)
type --> nvarchar(50)


My code is like:



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES ('" + yearcombo.Text + "','" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')", connection);


I get this error:




There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.








c# sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 12:38









marc_s

595k135 gold badges1139 silver badges1280 bronze badges




595k135 gold badges1139 silver badges1280 bronze badges










asked Mar 20 at 11:58









Darth SucukDarth Sucuk

10111 bronze badges




10111 bronze badges












  • I added an example of how the insert would be vulnerable to my answer, please have a look.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:23











  • WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

    – marc_s
    Mar 20 at 12:38

















  • I added an example of how the insert would be vulnerable to my answer, please have a look.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:23











  • WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

    – marc_s
    Mar 20 at 12:38
















I added an example of how the insert would be vulnerable to my answer, please have a look.

– Ashkan Mobayen Khiabani
Mar 20 at 12:23





I added an example of how the insert would be vulnerable to my answer, please have a look.

– Ashkan Mobayen Khiabani
Mar 20 at 12:23













WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

– marc_s
Mar 20 at 12:38





WHY do you store a year (which is clearly a numerical value) as nvarchar(4) ?!?!?! Use the most appropriate datatype - always - and here this would definitely be an INT (more than a nvarchar(4)) ....

– marc_s
Mar 20 at 12:38












2 Answers
2






active

oldest

votes


















11














This kind of insert is vulnerable to injection and as you have already discovered it is hard to add values like decimal, DateTime, ... to add columns, use Parameters.Add



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@year, @name,@rate,@type)", connection);

cmd.Parameters.Add("@year", SqlDbType.Int).Value = int.Parse(yearcombo.Text);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
cmd.Parameters.Add("@rate", SqlDbType.Decimal).Value = Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture);
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = "WORKERS";


Imagine that somebody changes the yearcombo value (as simply as using inspect element in chrome for example) and set it to:



"2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019,"



then your command text would be:



"insert into users (year,name,rate,type) (2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019," ,'" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')"





share|improve this answer

























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31











  • @DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:41












  • I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

    – Darth Sucuk
    Mar 20 at 12:47











  • just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:50











  • it not fix it I did you said but now on it get 37,62 to 3762,00

    – Darth Sucuk
    Mar 20 at 13:06


















7














You should use SqlParameter with a query containing parameters :
It should be like this :



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@YEAR, @NAME, @RATE, @TYPE)", connection);
cmd.Parameters.Add(new SqlParameter("@YEAR", yearcombo.Text));
cmd.Parameters.Add(new SqlParameter("@NAME", name.Text));
cmd.Parameters.Add(new SqlParameter("@RATE", Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture)));
cmd.Parameters.Add(new SqlParameter("@TYPE", "WORKERS"));


I didn't test it but it convert your parameter to a correct decimal for your query.
Also it is better to use SqlParameter for security issues (preventing SQL Injection)






share|improve this answer























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55260242%2fhow-to-insert-decimal-value-from-textbox-into-database-with-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









11














This kind of insert is vulnerable to injection and as you have already discovered it is hard to add values like decimal, DateTime, ... to add columns, use Parameters.Add



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@year, @name,@rate,@type)", connection);

cmd.Parameters.Add("@year", SqlDbType.Int).Value = int.Parse(yearcombo.Text);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
cmd.Parameters.Add("@rate", SqlDbType.Decimal).Value = Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture);
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = "WORKERS";


Imagine that somebody changes the yearcombo value (as simply as using inspect element in chrome for example) and set it to:



"2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019,"



then your command text would be:



"insert into users (year,name,rate,type) (2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019," ,'" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')"





share|improve this answer

























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31











  • @DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:41












  • I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

    – Darth Sucuk
    Mar 20 at 12:47











  • just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:50











  • it not fix it I did you said but now on it get 37,62 to 3762,00

    – Darth Sucuk
    Mar 20 at 13:06















11














This kind of insert is vulnerable to injection and as you have already discovered it is hard to add values like decimal, DateTime, ... to add columns, use Parameters.Add



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@year, @name,@rate,@type)", connection);

cmd.Parameters.Add("@year", SqlDbType.Int).Value = int.Parse(yearcombo.Text);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
cmd.Parameters.Add("@rate", SqlDbType.Decimal).Value = Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture);
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = "WORKERS";


Imagine that somebody changes the yearcombo value (as simply as using inspect element in chrome for example) and set it to:



"2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019,"



then your command text would be:



"insert into users (year,name,rate,type) (2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019," ,'" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')"





share|improve this answer

























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31











  • @DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:41












  • I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

    – Darth Sucuk
    Mar 20 at 12:47











  • just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:50











  • it not fix it I did you said but now on it get 37,62 to 3762,00

    – Darth Sucuk
    Mar 20 at 13:06













11












11








11







This kind of insert is vulnerable to injection and as you have already discovered it is hard to add values like decimal, DateTime, ... to add columns, use Parameters.Add



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@year, @name,@rate,@type)", connection);

cmd.Parameters.Add("@year", SqlDbType.Int).Value = int.Parse(yearcombo.Text);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
cmd.Parameters.Add("@rate", SqlDbType.Decimal).Value = Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture);
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = "WORKERS";


Imagine that somebody changes the yearcombo value (as simply as using inspect element in chrome for example) and set it to:



"2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019,"



then your command text would be:



"insert into users (year,name,rate,type) (2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019," ,'" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')"





share|improve this answer















This kind of insert is vulnerable to injection and as you have already discovered it is hard to add values like decimal, DateTime, ... to add columns, use Parameters.Add



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@year, @name,@rate,@type)", connection);

cmd.Parameters.Add("@year", SqlDbType.Int).Value = int.Parse(yearcombo.Text);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
cmd.Parameters.Add("@rate", SqlDbType.Decimal).Value = Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture);
cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = "WORKERS";


Imagine that somebody changes the yearcombo value (as simply as using inspect element in chrome for example) and set it to:



"2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019,"



then your command text would be:



"insert into users (year,name,rate,type) (2019,'a',1,'Something');insert into ARESTRICTEDTABLE(somefield) values('somecolumn'); update AnotherTable set SomeField = 'somevalue');, insert into users(year,name,rate,type),(2019," ,'" + name.Text + "','" + Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture) + ",'" + "WORKERS" + "')"






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 20 at 12:21

























answered Mar 20 at 12:05









Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani

23.9k19 gold badges68 silver badges126 bronze badges




23.9k19 gold badges68 silver badges126 bronze badges












  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31











  • @DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:41












  • I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

    – Darth Sucuk
    Mar 20 at 12:47











  • just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:50











  • it not fix it I did you said but now on it get 37,62 to 3762,00

    – Darth Sucuk
    Mar 20 at 13:06

















  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31











  • @DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:41












  • I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

    – Darth Sucuk
    Mar 20 at 12:47











  • just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

    – Ashkan Mobayen Khiabani
    Mar 20 at 12:50











  • it not fix it I did you said but now on it get 37,62 to 3762,00

    – Darth Sucuk
    Mar 20 at 13:06
















it gives me Error converting data type nvarchar to numeric.

– Darth Sucuk
Mar 20 at 12:31





it gives me Error converting data type nvarchar to numeric.

– Darth Sucuk
Mar 20 at 12:31













@DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

– Ashkan Mobayen Khiabani
Mar 20 at 12:41






@DarthSucuk As i don't know your exact table structure, somethings might not match it. for example if year column is a nvarchar, you should change the first parameter like this: cmd.Parameters.Add("@year", SqlDbType.NVarChar).Value = yearcombo.Text; and so on, check and make sure that your column types match the parameter type.

– Ashkan Mobayen Khiabani
Mar 20 at 12:41














I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

– Darth Sucuk
Mar 20 at 12:47





I fix it when I write 37,2 it gives an error but when I write 37.2 it inserts but the main problem is it stored with comma.How can I fix it?

– Darth Sucuk
Mar 20 at 12:47













just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

– Ashkan Mobayen Khiabani
Mar 20 at 12:50





just replace the comma with. before trying to convert it: Convert.ToDecimal(rate.Text.Replace(',','.'), CultureInfo.CurrentCulture)

– Ashkan Mobayen Khiabani
Mar 20 at 12:50













it not fix it I did you said but now on it get 37,62 to 3762,00

– Darth Sucuk
Mar 20 at 13:06





it not fix it I did you said but now on it get 37,62 to 3762,00

– Darth Sucuk
Mar 20 at 13:06













7














You should use SqlParameter with a query containing parameters :
It should be like this :



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@YEAR, @NAME, @RATE, @TYPE)", connection);
cmd.Parameters.Add(new SqlParameter("@YEAR", yearcombo.Text));
cmd.Parameters.Add(new SqlParameter("@NAME", name.Text));
cmd.Parameters.Add(new SqlParameter("@RATE", Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture)));
cmd.Parameters.Add(new SqlParameter("@TYPE", "WORKERS"));


I didn't test it but it convert your parameter to a correct decimal for your query.
Also it is better to use SqlParameter for security issues (preventing SQL Injection)






share|improve this answer























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31















7














You should use SqlParameter with a query containing parameters :
It should be like this :



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@YEAR, @NAME, @RATE, @TYPE)", connection);
cmd.Parameters.Add(new SqlParameter("@YEAR", yearcombo.Text));
cmd.Parameters.Add(new SqlParameter("@NAME", name.Text));
cmd.Parameters.Add(new SqlParameter("@RATE", Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture)));
cmd.Parameters.Add(new SqlParameter("@TYPE", "WORKERS"));


I didn't test it but it convert your parameter to a correct decimal for your query.
Also it is better to use SqlParameter for security issues (preventing SQL Injection)






share|improve this answer























  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31













7












7








7







You should use SqlParameter with a query containing parameters :
It should be like this :



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@YEAR, @NAME, @RATE, @TYPE)", connection);
cmd.Parameters.Add(new SqlParameter("@YEAR", yearcombo.Text));
cmd.Parameters.Add(new SqlParameter("@NAME", name.Text));
cmd.Parameters.Add(new SqlParameter("@RATE", Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture)));
cmd.Parameters.Add(new SqlParameter("@TYPE", "WORKERS"));


I didn't test it but it convert your parameter to a correct decimal for your query.
Also it is better to use SqlParameter for security issues (preventing SQL Injection)






share|improve this answer













You should use SqlParameter with a query containing parameters :
It should be like this :



SqlCommand cmd = new SqlCommand("INSERT INTO USERS(YEAR,NAME,RATE,TYPE) VALUES (@YEAR, @NAME, @RATE, @TYPE)", connection);
cmd.Parameters.Add(new SqlParameter("@YEAR", yearcombo.Text));
cmd.Parameters.Add(new SqlParameter("@NAME", name.Text));
cmd.Parameters.Add(new SqlParameter("@RATE", Convert.ToDecimal(rate.Text, CultureInfo.CurrentCulture)));
cmd.Parameters.Add(new SqlParameter("@TYPE", "WORKERS"));


I didn't test it but it convert your parameter to a correct decimal for your query.
Also it is better to use SqlParameter for security issues (preventing SQL Injection)







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 20 at 12:04









S. SchenkelS. Schenkel

1217 bronze badges




1217 bronze badges












  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31

















  • it gives me Error converting data type nvarchar to numeric.

    – Darth Sucuk
    Mar 20 at 12:31
















it gives me Error converting data type nvarchar to numeric.

– Darth Sucuk
Mar 20 at 12:31





it gives me Error converting data type nvarchar to numeric.

– Darth Sucuk
Mar 20 at 12:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55260242%2fhow-to-insert-decimal-value-from-textbox-into-database-with-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript