from dropdownlist to textbox and then datagrid

hello guys,

i am very new to vb.net, and i am making a webapplication .. a simple one ..

i have a dropdownlist, 2 textboxes and 1 datagrid.

the purpose of the ddl is to show some ID, once an ID is selected, it must update the 2 textboxes with values.. and it should update the datagrid as well with the id.

now let me show what i have...

Shared Function ID() As DataSet
        Dim FillID As DataSet = New DataSet
        Dim sql As String = ""
        sql = "SELECT * FROM testFirma ORDER BY CompanyID "
        FillID = SqlHelper.ExecuteDataset(AppKonfig.LogonToServer, CommandType.Text, sql)
        Return FillID
    End Function

this is the thing getting data to ddl,

as i am selecting * from CompanyID i assume it will also select companyName and CompanyAd
so once i select an ID in ddl, it should fill the two text boxes...

concerning the datagrid, we can wait with it until the first part is over...

hope someone can help me
LVL 1
Sam SungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

digitalZoCommented:
Where are you calling or referencing the dataset?

<<<<<sql = "SELECT * FROM testFirma ORDER BY CompanyID "
as i am selecting * from CompanyID i assume it will also select companyName and CompanyAd
so once i select an ID in ddl, it should fill the two text boxes...>>>>>

You don't need to call all the columns since you're just filling the dropdownlist, not the grid [for now].

So what you can do is:
sql = "SELECT CompanyID FROM testFirma ORDER BY CompanyID

And then on dropdownlist SelectedIndexChanged event, you can call the columns which you want to bind to the textboxes:



Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
  sQry = "select companyName, CompanyAd from testFirma where CompanyID ='" & DropDownlist1.SelectedValue & "'"
        da = New SqlDataAdapter(sQry, getConnectionString())
        da.Fill(ds)
 
       
            If ds.Tables(0).Rows.Item(0) Is Nothing Then
                labelerror.text = "No data"
            Else
                textbox1.Text = ds.Tables(0).Rows(0).Item(0).ToString
                textbox2.Text = ds.Tables(0).Rows(0).Item(1).ToString
            End If
        End If
 
End Sub
 
'GetconnectionString is defined as (assuming you have set the key in your web.config file under appsettings):
 
 Protected Shared Function getConnectionString() As String
 
        Return ConfigurationManager.AppSettings("LogonToServer")
    End Function

Open in new window

0
digitalZoCommented:
Make sure you set the AutoPostBack of the DropDownList to "True". Otherwise, it won't postback to the server.
0
Sam SungAuthor Commented:
i will try this right away and give you my feedback
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Sam SungAuthor Commented:
as i said, i am very new to this thing, so please forgive me for being stupid enough ..

my questions for the above post :

i just copied and pasted what ever u sended to me above,
sQry= Name is not Declared
getConnectionString is not declared
da and ds same case .. how do i solve it ?

yes i do have a <appSettings> in webconf where i have put the connection string for database.

0
Sam SungAuthor Commented:
i got this line solved
        da = New SqlDataAdapter(sQry, getConnectionString())
by typing

        da = New SqlClient.SqlDataAdapter(sQry, AppKonfig.LogonToServer())
0
digitalZoCommented:
You have to declare the variables before using them:

<<sQry= Name is not Declared>>

sQRY is a string which will store the query

<<getConnectionString is not declared>>

GetConnectionString is a function. I've posted the code above.

<<da and ds same case .. how do i solve it ?>>

da and ds are adapter and dataset respectively:

Here's the complete codE:

PS: I've filled the dropdownlist differently from the way you have, I haven't really tried your way, so I couldn't really say for sure whether it was correct or wrong. In the following code, I've done it my way. You can try it too.
Imports System.Data
Imports System.Data.SqlClient
 
Partial Class Default2
    Inherits System.Web.UI.Page
 
    Dim sqry As String
    Dim dr As SqlDataReader
    Dim da As SqlDataAdapter
    Dim ds As New DataSet
    Dim sqcon As SqlConnection
    Dim sqcom As SqlCommand
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not IsPostBack Then
            sqry = "SELECT CompanyID FROM testFirma ORDER BY CompanyID"
            da = New SqlDataAdapter(sqry, getConnectionString())
 
            da.Fill(ds)
            DropDownList1.DataSource = ds
            DropDownList1.DataTextField = "CompanyID"
            DropDownList1.DataValueField = "CompanyID"
 
            DropDownList1.DataBind()
 
        End If
 
 
    End Sub
 
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
      sQry = "select companyName, CompanyAd from testFirma where CompanyID ='" & DropDownlist1.SelectedValue & "'"
        da = New SqlDataAdapter(sQry, getConnectionString())
        da.Fill(ds)
 
       
            If ds.Tables(0).Rows.Item(0) Is Nothing Then
                labelerror.text = "No data"
            Else
                textbox1.Text = ds.Tables(0).Rows(0).Item(0).ToString
                textbox2.Text = ds.Tables(0).Rows(0).Item(1).ToString
            End If
        End If
 
    End Sub
 
    Protected Shared Function getConnectionString() As String
 
        Return ConfigurationManager.AppSettings("LogonToServer")
    End Function
End Class

Open in new window

0
digitalZoCommented:
Sorry, you don't need these two declarations:

   Dim sqcon As SqlConnection
    Dim sqcom As SqlCommand

So you can comment or remove them.
0
Sam SungAuthor Commented:
Your example works perfectly ... also much easier for me to understand the code :)

I know for 50 points I am asking too much, but please help me, and I will give u the needful amount of points when I have got answered my questions...

Now having this ID from ddl, how do I combine it to a data grid as well where it will show all the information in data grid where I can add and edit as well

i dont know if my question is clear..

when i select from ddl, it should also show things in datagrid
0
digitalZoCommented:
Before I answer - I would like one thing cleared-

You click on the dropdown, the data should bind to the grid? OR when you are binding the data to the dropdown and it should show the data on the grid too?

Does the grid display data based on dropdown filtering of the ID's?
0
Sam SungAuthor Commented:
the data display based on ddl filtering ID, Yes.

what does the term binding means, can u please explain ?
0
digitalZoCommented:
<<what does the term binding means, can u please explain ?>>

Binding means [i'll try to keep it as simple as possible] to "assign or attach the data to the grid or any other control".

For technical implication, you can have a look here: http://www.webopedia.com/TERM/B/bind.html

For "normal" meaning: http://www.thefreedictionary.com/bind

Back to the problem, the grid is being filtered by the ID generated by the dropdownlist. In that case, you can type the code as:

You just need to change the query and add these two lines:

 GridView1.DataSource = ds
 GridView1.DataBind()

*Change the Item number according to the position of the column as in the dataset which you want to bind to the textbox.

For example, if the data you want to bind to the textbox1 is the 3rd column in the dataset, then you need to code as:

 TextBox1.Text = ds.Tables(0).Rows(0).Item(2).ToString

we are giving 2 for the 3rd column because the row/column count in the dataset starts from 0. So, the first column will be at the index of 0, second column at the index of 1 and so on...

hope this helps.
  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
        sqry = "SELECT * FROM testFirma ORDER BY CompanyID='" & DropDownList1.SelectedValue.Trim & "'"
        da = New SqlDataAdapter(sqry, getConnectionString())
        da.Fill(ds)
 
        If ds.Tables(0).Rows.Item(0) Is Nothing Then
            Exit Sub
        Else
            TextBox1.Text = ds.Tables(0).Rows(0).Item(0).ToString
            TextBox2.Text = ds.Tables(0).Rows(0).Item(1).ToString
        End If
 
        GridView1.DataSource = ds
        GridView1.DataBind()
 
 
    End Sub

Open in new window

0
Sam SungAuthor Commented:
Working gr8 :)

now just last question, sorry for asking too much, but i feel i learn so much by asking instead of reading, I try these codes and it gives me a very good understanding of the code so i can build further on them.

Once again thanks being to kind,

My final question is,
Lets say i have another table, which also have an ID, now based on the ID I selected on the dropdown list, it should make a new query..

Like lets say I selected ID 4 from DropdownList

Then it ask the employee table and show all the data, with ID 4 .. This ID 4 should come by selecting from Dropdown list

Hope my question is clear,

And once again thanks for being so patience and so helpful.

0
digitalZoCommented:
<<<<<<<<<Lets say i have another table, which also have an ID, now based on the ID I selected on the dropdown list, it should make a new query..

Like lets say I selected ID 4 from DropdownList

Then it ask the employee table and show all the data, with ID 4 .. This ID 4 should come by selecting from Dropdown list>>>>>>>>>

Not sure, I understand it. Can you elaborate on it a little more?

Aren't you doing the same thing above? That is displaying the data in the grid by selecting the ID from the dropdownlist?
0
Sam SungAuthor Commented:
the grid should get the id number from the dropdownlist and run another sql query than the privious one..

the id number is actually Department number

so the gridview will be used for employee table

right now, the gridview is showing data from same table as dropdownlist,
i want to change that to another table ..

so the dropdownlist will get info from table testFirma
and the gridview will get info from table testemployee







0
digitalZoCommented:
Hmm..okay I get it now. Although, it's quite similar to binding the data to the TextBoxes. You can code it this way:

hth.
  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
        sqry = "select * from clientmst where clientid= '" & DropDownList1.SelectedValue.Trim & "'"
        da = New SqlDataAdapter(sqry, getConnectionString())
        da.Fill(ds)
 
        If ds.Tables(0).Rows.Item(0) Is Nothing Then
            Exit Sub
        Else
            TextBox1.Text = ds.Tables(0).Rows(0).Item(0).ToString
            TextBox2.Text = ds.Tables(0).Rows(0).Item(1).ToString
        End If
 
        sqry = ""
 
        ds.Clear()
 
        sqry = "select * from testemployee where companyid= '" & DropDownList1.SelectedValue.Trim & "'"
        da = New SqlDataAdapter(sqry, getConnectionString())
        da.Fill(ds)
 
        GridView1.DataSource = ds
        GridView1.DataBind()
 
 
    End Sub
 
 
''''Or if it gets too complicated you can create another Sub and call it in Dropdown event handler. Something like this:
 
 Protected Sub BindGrid()
        ds.Clear()
 
        sqry = ""
        sqry = "select * from testemployee where companyid= '" & DropDownList1.SelectedValue.Trim & "'"
        da = New SqlDataAdapter(sqry, getConnectionString())
        da.Fill(ds)
 
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub
 
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
        sqry = "select * from clientmst where clientid= '" & DropDownList1.SelectedValue.Trim & "'"
        da = New SqlDataAdapter(sqry, getConnectionString())
        da.Fill(ds)
 
        If ds.Tables(0).Rows.Item(0) Is Nothing Then
            Exit Sub
        Else
            TextBox1.Text = ds.Tables(0).Rows(0).Item(0).ToString
            TextBox2.Text = ds.Tables(0).Rows(0).Item(1).ToString
        End If
 
        BindGrid()
 
 
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sam SungAuthor Commented:
Yes creating another sub was the solution I understand most, so let me use it...

Everything was perfect, better than perfect... you have been so polite and so helpful.

Is it actually possible to put attributes ass ADD and Delete to the grid view? Or is it too complicated. If its complicated, is it better to use another component from the Toolbox.



0
digitalZoCommented:
There's no 'ADD' feature. For that, you'll have to workaround the grid to insert. As for Delete, you can do it by setting the AutoGenerateDeleteButton=true in the Properties of the Gridview. There's an EDIT button too if you want to edit that is.

  <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
        </asp:GridView>
0
Sam SungAuthor Commented:
 <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
        </asp:GridView>

this works perfect, and u please refer to some article or anything how i can make that ADD thing as well
0
digitalZoCommented:
hey, you know i gotta say you are asking too many questions for 50 points. ;-)

Two links:

http://www.gridviewguy.com/ArticleDetails.aspx?articleID=98

http://www.asp.net/learn/data-access/tutorial-53-vb.aspx

you'll have to workaround and modify/tweak the code a bit for it to work. goodluck! :-)
0
Sam SungAuthor Commented:
as i said in my previous post,
i already knew from the start that 50 points is too less for all the help you have done

is 250 Fair ?

once again thank you so much for all the help you have been giving .. i will stop my questions for now :P
0
digitalZoCommented:
i was kidding, mate. i was enjoying the QnA session. ;-)

you can allot 50 points or as much as you wish. i won't mind it. =)
0
Sam SungAuthor Commented:
:) Fair is fair , you have been so helpful .. i will put the max of 500 :)

thankyou so much :)
0
Sam SungAuthor Commented:
Very polite and kind, helpful user ... Hats off for his help :)
0
digitalZoCommented:
Thank you. And it was a pleasure to answer your questions. :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.