5 Replies - 579 Views - Last Post: 24 May 2013 - 01:01 PM

#1 VB_user  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-March 13

Retrievng data from MS SQLserver

Posted 24 May 2013 - 11:47 AM

Hi people, need help is using MS SQL server,
I have a table called CUSTTABLE within a database with two columns called cust_name (to hold their names) and cust_number (to hold their number) . both these columns have been populated and am trying to read data from here. cust_name has data type(Varchar(50) while cust_number has data type numeric (18,0) I am trying to display both customer's name and number by using their name
CODE
>>> select * from CUSTTABLE where cust_name = 'smith' <<<<<


this syntax doesn't work and brings up the error Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'smith'.

when I changed over and search using their number it works and brings the right results. here is the syntax that works
select * from CUSTTABLE where cust_number = '077885421' 


can anyone please advise on where am going wrong
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Retrievng data from MS SQLserver

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: Retrievng data from MS SQLserver

Posted 24 May 2013 - 12:08 PM

Moved to the MSSQL section.

A few things - are there records in 'cust_name' that are *EXACT* matches to "smith"? I am not talking "Johns Smith" or "d Smith" or event "Smith", but "smith"? If not then you need to start using the 'like' (instead of equals) and the wildcard %.

Also - when evaluating numbers against a number column it is not necessary to convert the number to a string by putting tick marks around it.
Was This Post Helpful? 0
  • +
  • -

#3 VB_user  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-March 13

Re: Retrievng data from MS SQLserver

Posted 24 May 2013 - 12:37 PM

Hi thanks for that I have used like instead of equals and now it displays the correct results..one more things I don't surpose you are familiar with using VB.NET. just because I want to use the syntax in a program that am making using VB.NET and I need a bit of help with that aswell.
just in case you are familiar with VB.NET, I have attached a screen print of the error that am getting and it shows the code as well. am still getting the same problem where I cant search by the name but I can search by the number.
cheer
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: Retrievng data from MS SQLserver

Posted 24 May 2013 - 12:51 PM

I don't see a screen shot anywhere.

Also, when you do add teh error, add the code in the post and not just the image.
Was This Post Helpful? 0
  • +
  • -

#5 VB_user  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-March 13

Re: Retrievng data from MS SQLserver

Posted 24 May 2013 - 12:55 PM

sorry about that here is all of the code

Imports System.Data.SqlClient
Public Class Update_contact

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim CONNECT As New SqlConnection("Server = darius_computer \ sqlexpress; Database = PHONE DIRECTORY; Trusted_connection =yes ")

        CONNECT.Open()
        Dim V_ds As DataSet
        Dim V_dt As DataTable
        Dim V_da As SqlDataAdapter


        V_ds = New DataSet
        V_dt = New DataTable

        V_ds.Tables.Add(V_dt)
        V_da = New SqlDataAdapter
        V_da = New SqlDataAdapter("select * from cust_name where number like " & search_name.Text, CONNECT)

        V_da.Fill(V_dt)


        DataGridView1.DataSource = V_dt.DefaultView
        CONNECT.Close()
    End Sub


the erros is highlighted at V_da.Fill(V_dt) and it says >>>>>>invalid column name 'smith<<<<<
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: Retrievng data from MS SQLserver

Posted 24 May 2013 - 01:01 PM

19	        V_da = New SqlDataAdapter("select * from cust_name where number like " & search_name.Text, CONNECT)


First you should be using the parameters instead of directly adding to the sql string.

Parameters clear up the need for having to add tick marks (which you are missing) to signify you want to compare a column with a string, and prevents odd ball sql injection attacks.

Second - you are missing the tickmarks around your textbox's string to make SQL know you are telling it is a string and not a column.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1