2 Replies - 481 Views - Last Post: 19 January 2013 - 07:58 PM Rate Topic: -----

#1 manikandan75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Number of query values and destination fields are not the same

Posted 18 January 2013 - 09:18 PM

Hi Experts, Im new to .net and im working on a project where i need to save and retrieve the image/picture or ole object data from mdb in my windows form in order to use it in my reports & print...i've been working on this for almost 3 weeks on this and i got this error " [b]Number of query values and destination fields are not the same." and this is my code: -----------


Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ray As String
ray = Me.cjTextBox.Text
Dim ray1 As String
ray1 = Me.ptTextBox.Text
Dim ans As String
Dim company, flg2, address, branch, pt, desc, flg, lang, cj, email, path As String
Dim amt As Integer



Me.TextBox5.Text = Me.pathTextBox.Text
Dim fs As New FileStream(TextBox5.Text, FileMode.Open, FileAccess.Read)
Dim bys As New BinaryReader(fs)
Dim bty(fs.Length) As Byte
bys.Read(bty, 0, fs.Length)
fs.Close()

Me.FlgTextBox.Text = "adfcup"
Me.Flg2TextBox.Text = "1"
If Me.RadioButton1.Checked = True Then
Me.LangTextBox.Text = "madrn"
ElseIf Me.RadioButton2.Checked = True Then
Me.LangTextBox.Text = "eng"
End If


Me.CompanyTextBox1.Text = Me.CompanyTextBox.Text
Me.AddressTextBox1.Text = Me.AddressTextBox.Text
Me.BranchTextBox1.Text = Me.BranchTextBox.Text
Me.ComboBox1.Text = Me.PayeeTextBox1.Text

company = CompanyTextBox1.Text
address = AddressTextBox1.Text
branch = BranchTextBox1.Text

pt = PtTextBox.Text
desc = DescTextBox.Text
flg = FlgTextBox.Text
flg2 = Flg2TextBox.Text
lang = LangTextBox.Text

cj = CjTextBox.Text
path = TextBox5.Text


If Me.ComboBox1.Text = Nothing Or Me.CjTextBox.Text = Nothing Or Me.PtTextBox.Text = Nothing Or Me.DescTextBox.Text = Nothing Then
MsgBox("Please fill all field")

ElseIf Len(Me.CjTextBox.Text) < 6 Then
MsgBox("Please cj your cj number, should be 6 digit")
Else


con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\first projet\user\user\mycollege.mdb")
con.Open()
str = "insert into cj values('" & CompanyTextBox1.Text & "','" & AddressTextBox1.Text & "','",'" & DateTimePicker1.Value & "','" & CjTextBox.Text & "','" & PtTextBox.Text & "','" & DescTextBox.Text & "','" & FlgTextBox.Text & "','" & LangTextBox.Text & "',@logo,'" & Flg2TextBox.Text & "')"
cmd = New OleDbCommand(str, con)
cmd.Parameters.Add("company", OleDbType.VarChar).Value = CompanyTextBox1.Text
cmd.Parameters.Add("address", OleDbType.VarChar).Value = AddressTextBox1.Text
cmd.Parameters.Add("date", OleDbType.Date).Value = DateTimePicker1.Text
cmd.Parameters.Add("cj", OleDbType.VarChar).Value = CjTextBox.Text
cmd.Parameters.Add("pt", OleDbType.VarChar).Value = PtTextBox.Text
cmd.Parameters.Add("desc", OleDbType.VarChar).Value = DescTextBox.Text
cmd.Parameters.Add("flg", OleDbType.VarChar).Value = FlgTextBox.Text
cmd.Parameters.Add("lang", OleDbType.VarChar).Value = LangTextBox.Text
cmd.Parameters.Add("path", OleDbType.VarChar).Value = TextBox5.Text
cmd.Parameters.AddWithValue("@logo", bty)
cmd.ExecuteNonQuery()


' Me.CompanyTextBox1.Text = Form2.CompanyTextBox.Text
'Me.AddressTextBox1.Text = Form2.AddressTextBox.Text


Me.CompanyTextBox2.Text = Me.CompanyTextBox.Text
Me.AddressTextBox2.Text = Me.AddressTextBox.Text

Me.DateTextBox.Text = Me.DateTimePicker1.Text


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''TO SAVE NUMBER'''''''''''''''''''''''''''''''''''''''''

ans = Val(ray) + Val(1)
If Len(ans) = 1 Then
Me.NumberTextBox.Text = "00000" & ans
ElseIf Len(ans) = 2 Then
Me.NumberTextBox.Text = "0000" & ans
ElseIf Len(ans) = 3 Then
Me.NumberTextBox.Text = "000" & ans
ElseIf Len(ans) = 4 Then
Me.NumberTextBox.Text = "00" & ans
ElseIf Len(ans) = 5 Then
Me.NumberTextBox.Text = "0" & ans
ElseIf Len(ans) = 6 Then
Me.NumberTextBox.Text = ans
End If
ans = Val(ray) + Val(1)
If Len(ans) = 1 Then
Me.numberTextBox1.Text = "00000" & ans
ElseIf Len(ans) = 2 Then
Me.numberTextBox1.Text = "0000" & ans
ElseIf Len(ans) = 3 Then
Me.numberTextBox1.Text = "000" & ans
ElseIf Len(ans) = 4 Then
Me.numberTextBox1.Text = "00" & ans
ElseIf Len(ans) = 5 Then
Me.numberTextBox1.Text = "0" & ans
ElseIf Len(ans) = 6 Then
Me.numberTextBox1.Text = ans
End If
Me.Validate()
Me.cjnoBindingSource.EndEdit()
Me.CjnoTableAdapter.Update(Me.CjnoDataSet.cjno)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Me.BindingNavigatorSaveItem.PerformClick()
Me.BindingNavigator1.AddNewItem.PerformClick()
Me.Validate()
Me.cjBindingSource.EndEdit()
Me.CjTableAdapter.Update(Me.CjDataSet.chq)
Me.cjBindingSource.AddNew()
Me.Label13.Text = Nothing
Me.Label14.Text = Nothing
Me.Label15.Text = Nothing
Me.Label16.Text = Nothing
Me.CjTextBox.Text = Me.NumberTextBox.Text
Me.ptTextBox.Text = Me.numberTextBox1.Text
Me.ComboBox1.Text = Nothing
Me.ComboBox1.Select()
End If '
End Sub

-----------------------------------
Any idea how to solve this ?

Is This A Good Question/Topic? 0
  • +

Replies To: Number of query values and destination fields are not the same

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Number of query values and destination fields are not the same

Posted 19 January 2013 - 07:49 PM

To start with.. Use code tags just like it says where you typed your question. It makes the code much easier to read for us if it is properly formatted.
:code:/>

Next: Debugging.
What does this error message mean?
Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

Lastly: Items 1-4 of the suggestions below to clean up your code.

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:

    Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Number of query values and destination fields are not the same

Posted 19 January 2013 - 07:58 PM

View Postmanikandan75, on 18 January 2013 - 10:18 PM, said:

Number of query values and destination fields are not the same."

str = "insert into cj values('" & CompanyTextBox1.Text & "','" & AddressTextBox1.Text & "','",'" & DateTimePicker1.Value & "','" & CjTextBox.Text & "','" & PtTextBox.Text & "','" & DescTextBox.Text & "','" & FlgTextBox.Text & "','" & LangTextBox.Text & "',@logo,'" & Flg2TextBox.Text & "')"
cmd = New OleDbCommand(str, con)
cmd.Parameters.Add("company", OleDbType.VarChar).Value = CompanyTextBox1.Text
cmd.Parameters.Add("address", OleDbType.VarChar).Value = AddressTextBox1.Text
cmd.Parameters.Add("date", OleDbType.Date).Value = DateTimePicker1.Text
cmd.Parameters.Add("cj", OleDbType.VarChar).Value = CjTextBox.Text
cmd.Parameters.Add("pt", OleDbType.VarChar).Value = PtTextBox.Text
cmd.Parameters.Add("desc", OleDbType.VarChar).Value = DescTextBox.Text
cmd.Parameters.Add("flg", OleDbType.VarChar).Value = FlgTextBox.Text
cmd.Parameters.Add("lang", OleDbType.VarChar).Value = LangTextBox.Text
cmd.Parameters.Add("path", OleDbType.VarChar).Value = TextBox5.Text
cmd.Parameters.AddWithValue("@logo", bty)


That's just a hot mess. And you don't seem to actually have parameter values in the string to then populate with the cmd.Parameters.Add values that you have below. Notice how you have companytextbox.text in both the string and as a parameter source? That's not right. if you are going to hard code in all the values then you don't set parameters. It looks liek you have zero fields and bunch of parameters to go into those zero fields.

But honestly its such a mess of "','"& stuff that I won't even try to figure it out. Maybe you just have a set of mis-matched quotes so you have 26 variables and 25 values, for example.

You could at lest cleanup the string concatenation with string.format.

You should also look at this tutorial on the right way to do parameterizations
Parameterizing Your SQL Queries: The RIGHT Way To Query A Database.

These tutorials might help as well.
[*]Q:... how to do x,y,z with a database {probably for the first time}...
A: Read this tutorial
Entire section of tutorials
Parameterizing Your SQL Queries: The RIGHT Way To Query A Database.
Using SqlDependency to monitor SQL database changes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1