Link to home
Start Free TrialLog in
Avatar of Sam Sung
Sam SungFlag for Denmark

asked on

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
Avatar of digitalZo
digitalZo
Flag of India image

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

Make sure you set the AutoPostBack of the DropDownList to "True". Otherwise, it won't postback to the server.
Avatar of Sam Sung

ASKER

i will try this right away and give you my feedback
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.

i got this line solved
        da = New SqlDataAdapter(sQry, getConnectionString())
by typing

        da = New SqlClient.SqlDataAdapter(sQry, AppKonfig.LogonToServer())
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

Sorry, you don't need these two declarations:

   Dim sqcon As SqlConnection
    Dim sqcom As SqlCommand

So you can comment or remove them.
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
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?
the data display based on ddl filtering ID, Yes.

what does the term binding means, can u please explain ?
<<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

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.

<<<<<<<<<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?
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







ASKER CERTIFIED SOLUTION
Avatar of digitalZo
digitalZo
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.



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>
 <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
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! :-)
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
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. =)
:) Fair is fair , you have been so helpful .. i will put the max of 500 :)

thankyou so much :)
Very polite and kind, helpful user ... Hats off for his help :)
Thank you. And it was a pleasure to answer your questions. :-)