Solved

popluate combobox fill gridview from webservice

Posted on 2008-06-14
4
712 Views
Last Modified: 2013-11-26
Create a window application to display sales for a selected store using multitier application. Allow the user to select store name from a drop down list.

Use sales and stores from the pubs database. sales table stor_id, ord_num, ord_date, qty, payterms, and title_id columns. Stores table has stor_id, stor_name, stor_address, city, state and zip columns.

Right the combobox is work. The GridView is displaying the id number I have in the sql statement. How do get it to display the results of the combobox?

Any help would be appreciated


<WebMethod()> Public Function dat(ByVal services As String) As DataSet
        Dim cn As New SqlConnection
        Dim da As SqlDataAdapter
        Dim strSelect As String
        Dim ds As DataSet
        Dim cnstring As String
        cnstring = "Data Source=.\SQLExpress; Initial Catalog=PUBS; Integrated Security=True;"
 
        cn = New SqlConnection(cnstring)
        cn.Open()
 
        'strSelect = "SELECT DISTINCT stores.stor_name, sales.stor_id FROM (stores INNER JOIN sales ON stores.stor_id = sales.stor_id)"
        strSelect = "SELECT * FROM Stores"
 
        da = New SqlDataAdapter(strSelect, cn)
        ds = New DataSet
        da.Fill(ds, "Stores")
        dat = ds
 
    End Function
 
    <WebMethod()> Public Function data(ByVal selected As String) As DataSet
        Dim cnstring As String
        Dim strSelect As String
        Dim ds As DataSet
        Dim da As SqlDataAdapter
 
        cnstring = "Data Source=.\SQLExpress; Initial Catalog=PUBS; Integrated Security=True;"
 
        strSelect = "SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id=7067"
 
        Dim cn As New SqlConnection(cnstring)
        cn.Open()
        Dim cmd As New SqlCommand(strSelect, cn)
 
        da = New SqlDataAdapter(strSelect, cn)
        ds = New DataSet
 
        da.Fill(ds, "Sales")
        Return ds
 
    End Function
 
 
 
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim combo As New localhost.Service()
        ComboBox1.DataSource = combo.dat.Tables(0)
        ComboBox1.DisplayMember = "stor_name"
        ComboBox1.ValueMember = "stor_id"
 
    End Sub
 
    Public Sub ComboBox1_SelectedIndexChanged(ByVal test As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 
        Dim combo1 As New localhost.Service()
        DataGridView1.DataSource = combo1.data
    End Sub
End Class

Open in new window

0
Comment
Question by:designaire
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 21788898
You are gonna have to pass the ComboBox1.selectedValue to your webservice call..
e.g. combo1.data(ComboBox1.selectedValue )

make sure that that inside your webservice query the selected value ends up in your WHERE clause.
e.g.
 strSelect =  string.Format("SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id={0}", selected)

0
 

Author Comment

by:designaire
ID: 21789204
I guess I know that's what I'm supposed to do but everytime I try it, I get some kind of error message.
I added the selection variable to the webservices though the public function and added it to the sql string. I"m doing something wrong because it doesn't work. It says there's an error message after the the = in the sql statement.

Thanks

selection = ComboBox1.SelectedValue.ToString
 
    <WebMethod()> Public Function data(ByVal selection As String) As 
     
        cnstring = "Data Source=.\SQLExpress; Initial Catalog=PUBS; Integrated Security=True;"
        'strSelect = "SELECT * from Sales"
 
 
        strSelect = "SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id=selection = ComboBox1.SelectedValue.ToString" & selection & ""

Open in new window

0
 

Author Comment

by:designaire
ID: 21789296
Sorry made a mistake on the sql statement...

selection = ComboBox1.SelectedValue.ToString
 
    <WebMethod()> Public Function data(ByVal selection As String) As
     
        cnstring = "Data Source=.\SQLExpress; Initial Catalog=PUBS; Integrated Security=True;"
        'strSelect = "SELECT * from Sales"
 
       strSelect = "SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id=" & selection & """"
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 125 total points
ID: 21791599
or you could copy/paste the line I put in there?

strSelect =  string.Format("SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id={0}", selected)

or when stor_id is a string:
strSelect =  string.Format("SELECT stor_id, ord_num, ord_date, qty, payterms, title_id FROM Sales WHERE stor_id='{0}' ", selected)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 65
reading excel file in .net 2 33
VB.NET Inline If statement 4 42
.NET - find out if application is running on Windows 2012 server 3 37
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question