I'm new in Visual Studio and as well as SQL Server. I got confused on how to insert data from visual studio application
I got an error of System.InvalidCastException. I am confused of what is the problem.
This is my code:
PatientInfoConnection.Open() 'open database connection
Dim sql As String = ""
sql = "insert into model (name, age, date_of_confinement, type_of_sickness, type_of_IVfluid, number_of_bottles, drop_rate)" & _
" values (@name, @age, @date_of_confinement, @type_of_sickness, @type_of_IV_fluid, @number_of_bottles, @drop_rate)"
Dim insert As New SqlCommand(sql, PatientInfoConnection)
insert.Parameters.AddWithValue("@name", txtName.Text)
insert.Parameters.AddWithValue("@age", nudAge.Value.ToString())
insert.Parameters.AddWithValue("@date_of_confinement", dtpDate.Value.ToString())
insert.Parameters.AddWithValue("@type_of_sickness", txtSickness.Text)
insert.Parameters.AddWithValue("@type_of_IV_fluid", txtFluid.Text)
insert.Parameters.AddWithValue("@number_of_bottles", txtBottle.Text)
insert.Parameters.AddWithValue("@drop_rate", nudDrop.Value.ToString())
insert.ExecuteNonQuery()
I am thinking of I got problem with the values. nudAge.Value is numeric up down, in my sql database, it is Age(int, null). I also have dtpDate.Value. A date time picker. I think I should not use ToString() on it but I dont know the syntax..
Can anyone help me? Any help would be appreciated. Thanks!
date_of_confinement is datetime in my db. Is the format should be like this? insert.Parameters.AddWithValue("@date_of_confinement", dtpDate.Value.ToShortDateString)
Also, I did insert.Parameters.AddWithValue("@number_of_bottles", Int32.Parse(txtBottle.Text))
but I still got the error. Im confused. Thanks for your help!
this what i did now... insert.Parameters.AddWithValue("@date_of_confinement", Date.Parse(dtpDate.Value))
I also made changes like:
insert.Parameters.AddWithValue("@age", Convert.ToInt32(txtAge.Text))
insert.Parameters.AddWithValue("@number_of_bottles", Convert.ToInt32(txtBottle.Text))
insert.Parameters.AddWithValue("@drop_rate", Convert.ToInt32(txtDrop.Text))
I dont have error anymore but when I click save, it didnt do anything even close the form and open the other form. Didn't do anything.
Thanks for your help! n.n