Connecting Tech Pros Worldwide Forums | Help | Site Map

VB.NET : Trouble inserting data into database

Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6
#1: Mar 14 '08
hello sir
i am making project on vb.net language..and project is on hospital management..its major project..and too many fields are present in its data base..i was connecting data base in forms but there is coming error..SIR PLEASE SORT OUT ERROR FROM MY COADING..what can i do to solve this error please give me sugession or if you can send me coading for adding information from form.than i will be thankfull to u..i have written all fields which has present in my database please help me..

error coming - too many parameters.
I HAVE MADE DATABASE ON MS ACCESS

SOURCE CODE -
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As New OleDbCommand("insert into table1 values(@date,@firstname,@middlename,@lastname,@maritalstatus,@sex,@feetype,
  2. @mothersName,@fathersname,@dd,@mm,@yy,@age,@residentialaddress,@city,@state,@country,@pincode,@religion,@bloodgroup,@telephoneno, @mobile,@pager,@email)", con)
  3.  
  4.         cmd.parameters.addwithvalue("@date", TextBox2.Text)
  5.         cmd.Parameters.AddWithValue("@firstname", TextBox14.Text)
  6.         cmd.Parameters.AddWithValue("@middlename", TextBox4.Text)
  7.         cmd.Parameters.AddWithValue("@lastname", TextBox5.Text)
  8.         cmd.Parameters.AddWithValue("@maritalstatus", ComboBox1.Text)
  9.         cmd.Parameters.AddWithValue("@sex", ComboBox2.Text)
  10.         cmd.Parameters.AddWithValue("@feetype", ComboBox3.Text)
  11.         cmd.Parameters.AddWithValue("@mothersname", TextBox6.Text)
  12.         cmd.Parameters.AddWithValue("@fathersname", TextBox7.Text)
  13.         cmd.Parameters.AddWithValue("@dd", ComboBox4.Text)
  14.         cmd.Parameters.AddWithValue("@mm", ComboBox5.Text)
  15.         cmd.Parameters.AddWithValue("@yy", ComboBox6.Text)
  16.         cmd.Parameters.AddWithValue("@age", TextBox8.Text)
  17.         cmd.Parameters.AddWithValue("@residentialaddress", TextBox9.Text)
  18.         cmd.Parameters.AddWithValue("@city", ComboBox7.Text)
  19.         cmd.Parameters.AddWithValue("@state", ComboBox8.Text)
  20.         cmd.Parameters.AddWithValue("@country", ComboBox9.Text)
  21.         cmd.Parameters.AddWithValue("@pincode", TextBox3.Text)
  22.         cmd.Parameters.AddWithValue("@religion", ComboBox10.Text)
  23.         cmd.Parameters.AddWithValue("@bloodgroup", ComboBox11.Text)
  24.         cmd.Parameters.AddWithValue("@telephoneno", TextBox11.Text)
  25.         cmd.Parameters.AddWithValue("@mobile", TextBox10.Text)
  26.         cmd.Parameters.AddWithValue("@pager", TextBox12.Text)
  27.         cmd.Parameters.AddWithValue("@email", TextBox13.Text)
  28.         con.Open()
  29.         cmd.ExecuteNonQuery()
  30.         con.Close()
  31.         MsgBox("record added")
  32.  
  33.     End Sub

Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6
#2: Mar 14 '08

re: VB.NET : Trouble inserting data into database


sir i am making project on hospital management..the language is vb.net..i have tried to do databese connecting but there is coming errors..and the error is coming just because of too many parameters..so many fields are present in my database that is why it is not connecting..i am sendimg u fields i request to u if u can send me coading for adding information of form i will be thankfull to u please help me..
fields is -
date
firstname
middlename
lastname, maritalstatus,sex,feetype,mothersName,fathersname, dd,mm,yy,age,residentialaddress,city,state,country ,pincode,religion,bloodgroup,telephoneno, mobile,pager,email

its form's field name..and when we enter information in it..information should be save in database..so please send me coading of it.i have made database on ms access..u can see my coading which i have done on project..but its not working..

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  2.         Dim cmd As New OleDbCommand("insert into table1 values(@date,@firstname,@middlename,@lastname,@maritalstatus,@sex,@feetype,@mothersName,@fathersname,@dd,@mm,@yy,@age,@residentialaddress,@city,@state,@country,@pincode,@religion,@bloodgroup,@telephoneno, @mobile,@pager,@email)", con)
  3.         cmd.Parameters.AddWithValue("@date", TextBox2.Text)
  4.         cmd.Parameters.AddWithValue("@firstname", TextBox14.Text)
  5.         cmd.Parameters.AddWithValue("@middlename", TextBox4.Text)
  6.         cmd.Parameters.AddWithValue("@lastname", TextBox5.Text)
  7.         cmd.Parameters.AddWithValue("@maritalstatus", ComboBox1.Text)
  8.         cmd.Parameters.AddWithValue("@sex", ComboBox2.Text)
  9.         cmd.Parameters.AddWithValue("@feetype", ComboBox3.Text)
  10.         cmd.Parameters.AddWithValue("@mothersname", TextBox6.Text)
  11.         cmd.Parameters.AddWithValue("@fathersname", TextBox7.Text)
  12.         cmd.Parameters.AddWithValue("@dd", ComboBox4.Text)
  13.         cmd.Parameters.AddWithValue("@mm", ComboBox5.Text)
  14.         cmd.Parameters.AddWithValue("@yy", ComboBox6.Text)
  15.         cmd.Parameters.AddWithValue("@age", TextBox8.Text)
  16.         cmd.Parameters.AddWithValue("@residentialaddress", TextBox9.Text)
  17.         cmd.Parameters.AddWithValue("@city", ComboBox7.Text)
  18.         cmd.Parameters.AddWithValue("@state", ComboBox8.Text)
  19.         cmd.Parameters.AddWithValue("@country", ComboBox9.Text)
  20.         cmd.Parameters.AddWithValue("@pincode", TextBox3.Text)
  21.         cmd.Parameters.AddWithValue("@religion", ComboBox10.Text)
  22.         cmd.Parameters.AddWithValue("@bloodgroup", ComboBox11.Text)
  23.         cmd.Parameters.AddWithValue("@telephoneno", TextBox11.Text)
  24.         cmd.Parameters.AddWithValue("@mobile", TextBox10.Text)
  25.         cmd.Parameters.AddWithValue("@pager", TextBox12.Text)
  26.         cmd.Parameters.AddWithValue("@email", TextBox13.Text)
  27.         con.Open()
  28.         cmd.ExecuteNonQuery()
  29.         con.Close()
  30.         MsgBox("record added")
  31.  
  32.     End Sub
thanking u
VBWheaties's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 145
#3: Mar 14 '08

re: VB.NET : Trouble inserting data into database


Data integrity is probably the issue. Make sure the values typed in the textboxes are allowed for the underlying data fields datatype. For example, if it is expecting a number, textboxes all return string values and it will error out.

Also, I would get rid of command objects in this case. You just dont need them for what you are doing.

Create a single INSERT statement like you did but remove the "@" place holders and put actual values.

For example:

INSERT INTO MyTable (Field1, Field2, Field3)
VALUES ('Value of field1', 'Value of field2', 'Value of field 3')
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#4: Mar 14 '08

re: VB.NET : Trouble inserting data into database


Quote:

Originally Posted by VBWheaties

Also, I would get rid of command objects in this case. You just dont need them for what you are doing.

I dunno, using a command object like that is safer. It forces correct datatypes and helps prevent against sql injection.


As for the problem, I don't think that is a valid SQL INSERT statement?
VBWheaties's Avatar
Familiar Sight
 
Join Date: Feb 2008
Posts: 145
#5: Mar 14 '08

re: VB.NET : Trouble inserting data into database


Quote:

Originally Posted by Plater

I dunno, using a command object like that is safer. It forces correct datatypes and helps prevent against sql injection.


As for the problem, I don't think that is a valid SQL INSERT statement?

You are right. In this case, it's a toss up between letting the code handle exceptions or, what I like to do, check the data integrity before sending it to the db.
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#6: Mar 15 '08

re: VB.NET : Trouble inserting data into database


Quote:

Originally Posted by VBWheaties

You are right. In this case, it's a toss up between letting the code handle exceptions or, what I like to do, check the data integrity before sending it to the db.

The problems are:

a). In your SQL statement you don't specify the fields to insert into - consequently you are required to enter all fields for the table you're inserting into. This means that if extra fields are added in your database, your query will then crash, which is what I suspect is happening to you. Try the following format:

Expand|Select|Wrap|Line Numbers
  1. Dim sSql As String = "Insert Into MyTable(Field1, Field2, Field3) Values(@Data1, @Data2, @Data3)"
  2. Dim oCmd As New SqlCommand(sSQL, oCon)
b). Your SqlParameter instances don't need the @ symbol when you're creating your parameter object:

Expand|Select|Wrap|Line Numbers
  1. oCmd.Parameters.AddWithValue("Data1", TextBox1.Text)
  2. oCmd.Parameters.AddWithValue("Data2", TextBox2.Text)
  3. oCmd.Parameters.AddWithValue("Data3", TextBox3.Text)
Notice how in my SQL statement I specify the placeholders using the @ symbol, but when I create my SqlParameter instances, I don't use the @ symbol.

If you correct both of these issues in your code, it should fix your problem.
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,569
#7: Mar 17 '08

re: VB.NET : Trouble inserting data into database


Problem is not with number of parameters.

Which line of code is creating the error ?
Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6
#8: Mar 19 '08

re: VB.NET : Trouble inserting data into database


hello sir

i want to know,after inserting the data in the textbox, how can we add that data in the database with the click of a button?
sir i have written code for this work please correct my coding..
error coming - "Data type mismatch in criteria expression"

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

cmd.Parameters.AddWithValue("@date", TextBox2.Text)
cmd.Parameters.AddWithValue("@uhid.textbox1.text)
cmd.Parameters.AddWithValue("@first name,TextBox14.Text)
cmd.Parameters.AddWithValue("@middle name",TextBox4.Text)
cmd.Parameters.AddWithValue( "@last name",TextBox5.Text)
cmd.Parameters.AddWithValue("@marital status",ComboBox1.Text)
cmd.Parameters.AddWithValue("@sex",ComboBox2.Text)
cmd.Parameters.AddWithValue("@fee type",ComboBox3.Text)
cmd.Parameters.AddWithValue("@mother's Name",TextBox6.Text)
cmd.Parameters.AddWithValue("@father's name",TextBox7.Text)
cmd.Parameters.AddWithValue("@dd",ComboBox4.Text)
cmd.Parameters.AddWithValue("@mm",ComboBox5.Text)
cmd.Parameters.AddWithValue("@yy",ComboBox6.Text)
cmd.Parameters.AddWithValue("@age",TextBox8.Text)
cmd.Parameters.AddWithValue("@residential address"TextBox9.Text)
cmd.Parameters.AddWithValue("@city"combobox7.text)
cmd.Parameters.AddWithValue("@state",combobox8.tex t)
cmd.Parameters.AddWithValue ("@country",combobox9.text)
cmd.Parameters.AddWithValue("@pincode""TextBox3.Te xt)
cmd.Parameters.AddWithValue("@region",combox10.tex t)
cmd.Parameters.AddWithValue ("@blood group",combobox11.text)
cmd.Parameters.AddWithValue("@telephone no",textbox11.text)
cmd.Parameters.AddWithValue ("@mobile",textbox10.text)
cmd.Parameters.AddWithValue("@pager",TextBox12.Tex t)
cmd.Parameters.AddWithValue("@email",textbox13.tex t)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
MsgBox("record added")

End Sub
Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6
#9: Mar 19 '08

re: VB.NET : Trouble inserting data into database


hello sir
i want to know after inserting data in the textbox than how can we add this data to the database.with the help of click of button.

sir i have written coad for this work please check the coding

error coming - Data type mismatch in criteria expression

please solve my problem

source code -
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  2.  
  3. cmd.Parameters.AddWithValue("@date", TextBox2.Text)
  4. cmd.Parameters.AddWithValue("@uhid.textbox1.text)
  5. cmd.Parameters.AddWithValue("@first name,TextBox14.Text)
  6. cmd.Parameters.AddWithValue("@middle name",TextBox4.Text)
  7. cmd.Parameters.AddWithValue( "@last name",TextBox5.Text) 
  8. cmd.Parameters.AddWithValue("@marital status",ComboBox1.Text)
  9. cmd.Parameters.AddWithValue("@sex",ComboBox2.Text)
  10. cmd.Parameters.AddWithValue("@fee type",ComboBox3.Text)
  11. cmd.Parameters.AddWithValue("@mother's Name",TextBox6.Text)
  12. cmd.Parameters.AddWithValue("@father's name",TextBox7.Text)
  13. cmd.Parameters.AddWithValue("@dd",ComboBox4.Text)
  14. cmd.Parameters.AddWithValue("@mm",ComboBox5.Text)
  15. cmd.Parameters.AddWithValue("@yy",ComboBox6.Text)
  16. cmd.Parameters.AddWithValue("@age",TextBox8.Text)
  17. cmd.Parameters.AddWithValue("@residential address"TextBox9.Text)
  18. cmd.Parameters.AddWithValue("@city"combobox7.text)
  19. cmd.Parameters.AddWithValue("@state",combobox8.text)
  20. cmd.Parameters.AddWithValue ("@country",combobox9.text) 
  21. cmd.Parameters.AddWithValue("@pincode""TextBox3.Text)
  22. cmd.Parameters.AddWithValue("@region",combox10.text)
  23. cmd.Parameters.AddWithValue ("@blood group",combobox11.text)
  24. cmd.Parameters.AddWithValue("@telephone no",textbox11.text)
  25. cmd.Parameters.AddWithValue ("@mobile",textbox10.text)
  26. cmd.Parameters.AddWithValue("@pager",TextBox12.Text)
  27. cmd.Parameters.AddWithValue("@email",textbox13.text)
  28. con.Open()
  29. cmd.ExecuteNonQuery()
  30. con.Close()
  31. MsgBox("record added")
  32.  
  33. End Sub
Newbie
 
Join Date: Mar 2008
Location: delhi
Posts: 6
#10: Mar 19 '08

re: VB.NET : Trouble inserting data into database


hello sir
i want to know after inserting data in the textbox than how can we add this data to the database.with the help of click of button.
i m making project in visual studio 2005
backend - ms access

sir i have written coad for this work please check the coding

error coming - Data type mismatch in criteria expression

please solve my problem

source code -
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  2.  
  3. cmd.Parameters.AddWithValue("@date", TextBox2.Text)
  4. cmd.Parameters.AddWithValue("@uhid.textbox1.text)
  5. cmd.Parameters.AddWithValue("@first name,TextBox14.Text)
  6. cmd.Parameters.AddWithValue("@middle name",TextBox4.Text)
  7. cmd.Parameters.AddWithValue( "@last name",TextBox5.Text) 
  8. cmd.Parameters.AddWithValue("@marital status",ComboBox1.Text)
  9. cmd.Parameters.AddWithValue("@sex",ComboBox2.Text)
  10. cmd.Parameters.AddWithValue("@fee type",ComboBox3.Text)
  11. cmd.Parameters.AddWithValue("@mother's Name",TextBox6.Text)
  12. cmd.Parameters.AddWithValue("@father's name",TextBox7.Text)
  13. cmd.Parameters.AddWithValue("@dd",ComboBox4.Text)
  14. cmd.Parameters.AddWithValue("@mm",ComboBox5.Text)
  15. cmd.Parameters.AddWithValue("@yy",ComboBox6.Text)
  16. cmd.Parameters.AddWithValue("@age",TextBox8.Text)
  17. cmd.Parameters.AddWithValue("@residential address"TextBox9.Text)
  18. cmd.Parameters.AddWithValue("@city"combobox7.text)
  19. cmd.Parameters.AddWithValue("@state",combobox8.text)
  20. cmd.Parameters.AddWithValue ("@country",combobox9.text) 
  21. cmd.Parameters.AddWithValue("@pincode""TextBox3.Text)
  22. cmd.Parameters.AddWithValue("@region",combox10.text)
  23. cmd.Parameters.AddWithValue ("@blood group",combobox11.text)
  24. cmd.Parameters.AddWithValue("@telephone no",textbox11.text)
  25. cmd.Parameters.AddWithValue ("@mobile",textbox10.text)
  26. cmd.Parameters.AddWithValue("@pager",TextBox12.Text)
  27. cmd.Parameters.AddWithValue("@email",textbox13.text)
  28. con.Open()
  29. cmd.ExecuteNonQuery()
  30. con.Close()
  31. MsgBox("record added")
  32.  
  33. End Sub
nateraaaa's Avatar
Expert
 
Join Date: May 2007
Location: Illinois
Posts: 663
#11: Mar 19 '08

re: VB.NET : Trouble inserting data into database


The type you pass to your stored procedure must match the type for the parameter. You are getting this error because you are passing a string when your proc expects a different type. You will need to Convert the text from your textboxes to match the type that your parameter expects.

Expand|Select|Wrap|Line Numbers
  1.  ("@Parameter1", Convert.ToInt32(TextBox1.Text);
Nathan
nateraaaa's Avatar
Expert
 
Join Date: May 2007
Location: Illinois
Posts: 663
#12: Mar 19 '08

re: VB.NET : Trouble inserting data into database


Please do not post duplicate questions. This violates the posting guidelines.


Nathan
Member
 
Join Date: Mar 2008
Posts: 36
#13: Mar 19 '08

re: VB.NET : Trouble inserting data into database


Just match the type with your database scheme ...
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#14: Mar 19 '08

re: VB.NET : Trouble inserting data into database


Quote:

Originally Posted by nateraaaa

Please do not post duplicate questions. This violates the posting guidelines.


Nathan

Please read the Posting guidelines. Specifically the section on Do not double post your question.

I have merged all of your duplicate threads into this one and I am closing this thread.

You can see the solution to your problem in your other thread that has the answers. That is where you can continue to receive help on your problem.

-Moderator Frinny
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#15: Mar 20 '08

re: VB.NET : Trouble inserting data into database


I split off your question from the articles section (do not post questions there) and merged a number of your similar threads. Please don't post the same question over and over again.


MODERATOR
Reply