By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,930 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,930 IT Pros & Developers. It's quick & easy.

VB.NET : Trouble inserting data into database

P: 6
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
Mar 14 '08 #1
Share this Question
Share on Google+
14 Replies


P: 6
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
Mar 14 '08 #2

VBWheaties
100+
P: 145
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')
Mar 14 '08 #3

Plater
Expert 5K+
P: 7,872
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?
Mar 14 '08 #4

VBWheaties
100+
P: 145
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.
Mar 14 '08 #5

balabaster
Expert 100+
P: 797
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.
Mar 14 '08 #6

debasisdas
Expert 5K+
P: 8,127
Problem is not with number of parameters.

Which line of code is creating the error ?
Mar 17 '08 #7

P: 6
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
Mar 19 '08 #8

P: 6
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
Mar 19 '08 #9

P: 6
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
Mar 19 '08 #10

nateraaaa
Expert 100+
P: 663
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
Mar 19 '08 #11

nateraaaa
Expert 100+
P: 663
Please do not post duplicate questions. This violates the posting guidelines.


Nathan
Mar 19 '08 #12

P: 36
Just match the type with your database scheme ...
Mar 19 '08 #13

Frinavale
Expert Mod 5K+
P: 9,731
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
Mar 19 '08 #14

Plater
Expert 5K+
P: 7,872
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
Mar 20 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.