Answer the question
In order to leave comments, you need to log in
How to pass double value to database?
Hello there is a local database created in MVS. You need to put a real value in it. I tried to make the data type float, decimal, numeric in the database, without .Replace(",", ".") for the field value does not want to write them to the database. And with .Replace(",", ".") rounds the number (or cuts off everything after "," , apparently). I tried Parameterized queries, it doesn't work (maybe I didn't try it correctly).
If we remove Replace, then com.ExecuteNonQuery(); throws the following error:
Unhandled exception of type 'System.Data.SqlClient.SqlException' in System.Data.dll
Additional information: Error converting data type nvarchar to float.
CREATE TABLE [dbo].[t_stoimost] (
[ID_stoimost] INT IDENTITY (1, 1) NOT NULL,
[mintonn] FLOAT NOT NULL,
[maxtonn] FLOAT NOT NULL,
[tonn] NVARCHAR (50) NOT NULL,
[cenakm] FLOAT NOT NULL,
PRIMARY KEY CLUSTERED ([ID_stoimost] ASC)
);
SqlConnection con;
SqlCommand com;
public void Connection()
{
string connect = @"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=|DataDirectory|\Database01.mdf;
Integrated Security=True";
con = new SqlConnection(connect);
con.Open();
}
Connection();
com = con.CreateCommand();
string tonn = addMin_TB.Text + " - " + addMax_TB.Text;
com.CommandText = "INSERT INTO dbo.t_stoimost (mintonn, maxtonn, tonn, cenakm) VALUES (N'" + addMin_TB.Text.ToString(CultureInfo.InvariantCulture).Replace(",", ".") + "',N'" + addMax_TB.Text.ToString(CultureInfo.InvariantCulture).Replace(",", ".") + "',N'" + tonn + "',N'" + addCena_TB.Text.ToString(CultureInfo.InvariantCulture).Replace(",", ".") + "')";
com.ExecuteNonQuery();
con.Close();
t_stoimostTableAdapter.Fill(database01DataSet.t_stoimost);
t_stoimostDataGridView.Update();
Answer the question
In order to leave comments, you need to log in
Cast types before insertion, and inserting directly from the interface is also not a very good idea.
Corrected the variables, you seem to have a form there. Look at my clearer and easier.
com = con.CreateCommand();
var addMin_TB_Text = "10,77777";
var min = float.Parse(addMin_TB_Text);
var addMax_TB_Text = "20,77777";
var max = float.Parse(addMax_TB_Text);
var addCena_TB_Text = "33,3";
var cena = float.Parse(addCena_TB_Text);
var tonn = addMin_TB_Text + " - " + addMax_TB_Text;
com.CommandText = @"INSERT INTO t_stoimost (mintonn, maxtonn, tonn, cenakm)
VALUES ( @mintonn, @maxtonn, @tonn, @cenakm )";
com.Parameters.AddWithValue("@mintonn", min);
com.Parameters.AddWithValue("@maxtonn", max);
com.Parameters.AddWithValue("@tonn", tonn);
com.Parameters.AddWithValue("@cenakm", cena);
com.ExecuteNonQuery();
con.Close();
Read the exception text again.
There it is written in English and white that it was not possible to convert a string (nvarchar) to float. And now let's go to your code and see that you are really trying to write string values from your TextBoxes into float fields (except for the tonn field, you have it in the database as a string, everything should be fine with it). You need to convert these string values to double. For example apply double.Parse(string).
Well, then you have to "squat" with separators =) Good luck!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question