Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2006, 06:11 PM
Authorized User
 
Join Date: Jan 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default retrieveing value through dataset

I want to get the SID of the selected SNAME which is displayed in the dropwdownliast.I no throught dropdownlist1.selecteditem.text gives the value of the selected text.According to the selected text in the dropdownlist,i want to store the values of SID into a variable.How do i retrieve the values through dataset.

dim para as string
         Dim strConnection As String = "workstation id=SERVER;packet size=4096;user id=sa;data source=server;persist secu" & _
        "rity info=True;initial catalog=students;password=Admin"
        Dim cn As SqlConnection = New SqlConnection(strConnection)
        cn.Open()

        dim strSelect As String = _
        "SELECT SNAME,SID FROM students"
        Dim da As SqlDataAdapter = New SqlDataAdapter(strSelect, cn)
        Dim autogen As New SqlCommandBuilder(da)
        Dim ds As DataSet = New DataSet
        da.Fill(ds,"students")
        DropDownList1.DataSource = ds
        DropDownList1.DataMember = ds.Tables("students").ToString
        DropDownList1.DataValueField = "SNAME"
        DropDownList1.DataBind()
 
Old January 16th, 2006, 03:37 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


Hi,

Use this method instead of data set.

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

   Dim sqlcommand As New SqlCommand("SELECT SID FROM students WHERE SNAME= '" & DropDownList1.Text & "'", dbconnection)
   Dim SName As String = sqlcommand.ExecuteScalar()

   MsgBox(SName)

End Sub



Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   Dim sqlcommand As New SqlCommand("SELECT SNAME FROM students", dbconnection)
   Dim myReader As SqlDataReader = sqlcommand.ExecuteReader()

   While myReader.Read()
       ComboBox1.Items.Add(myReader("SNAME"))
   End While

End Sub


Please post if you need more information.
Cheers,
   nalla
 
Old January 17th, 2006, 08:50 PM
Registered User
 
Join Date: Jan 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You may want to consider using prepared statement instead of the …
Dim sqlcommand As New SqlCommand("SELECT SID FROM students WHERE SNAME= '" & DropDownList1.Text & "'", dbconnection)

It is vulnerable to Sql injection (if this is a web app) and the statement will fail if the DropwonList1.Text contain an apostrophe (‘).

May not be a big issue in this case but in general you should be careful about stringing together SQL statements. Personally I do it from time to time, but then I use a general purpose function that takes care of the single apostrophe issue.



Best Regards
Georg Jansen
http://www.l4ndash.com - Log4net Dashboard and Log Viewer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill(dataset) or dataset.load() salemkoten SQL Server 2005 1 November 2nd, 2006 11:04 PM
Converting a untyped dataset to a typed dataset daphnean Visual Studio 2005 0 July 13th, 2006 01:16 AM
retrieveing values from db pettrer ASP.NET 2.0 Basics 8 April 8th, 2006 03:56 AM
retrieveing a numeric value into a "variable" grant6607 XSLT 2 May 11th, 2005 11:08 AM
Re: SQL Server dataset to ACCESS dataset dazzer ADO.NET 0 March 22nd, 2004 05:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.