Note that there are some explanatory texts on larger screens.

plurals
  1. POA first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll in vb.NET
    text
    copied!<p>I'm new in Visual Studio and as well as SQL Server. I got confused on how to insert data from visual studio application</p> <p>I got an error of System.InvalidCastException. I am confused of what is the problem. </p> <p>This is my code:</p> <pre><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)" &amp; _ " 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() </code></pre> <p>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.. </p> <p>Can anyone help me? Any help would be appreciated. Thanks!</p> <p>date_of_confinement is datetime in my db. Is the format should be like this? insert.Parameters.AddWithValue("@date_of_confinement", dtpDate.Value.ToShortDateString) </p> <p>Also, I did insert.Parameters.AddWithValue("@number_of_bottles", Int32.Parse(txtBottle.Text)) </p> <p>but I still got the error. Im confused. Thanks for your help!</p> <p>this what i did now... insert.Parameters.AddWithValue("@date_of_confinement", Date.Parse(dtpDate.Value))</p> <p>I also made changes like: </p> <pre><code> 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)) </code></pre> <p>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. </p> <p>Thanks for your help! n.n</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload