GetValue

eramgarden

Contributor
Joined
Mar 8, 2004
Messages
579
I'm reading a database field of "Text" into my datagrid..and I'm using a datareader and "Getvalue"..

dr.getValue(1)

Code DOES read this value back but I came across a website last night that said to use something else when reading "TEXT" values from the database ( cant find that website)...

Is this correct what I'm doing or should I be using GetXXX something else?
 
Never used a GetValue method. Here is how I usually appoach it;



<%@ Import Namespace = "System.Data.SqlClient" %>
<html>
<head>
<title>Sample ASP.NET Page</title>
</head>

<script language = "VB" runat = "server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim ConnStr as String
ConnStr = "Data Source=localhost; Integrated Security=SSPI;"
ConnStr = ConnStr + "Initial Catalog=pubs"
Dim conn As SqlConnection = New SqlConnection(ConnStr)
Try
conn.Open()
Dim CmdStr As String
CmdStr = "SELECT au_id,au_lname,au_fname FROM Authors"
Dim cmd as SqlCommand = New SqlCommand(CmdStr,conn)
Dim reader As SqlDataReader = cmd.ExecuteReader()
Do While reader.Read()
Authors.Items.Add(New ListItem(reader.Item("au_fname") _
+ " " + reader.Item("au_lname"),reader.Item("au_id")))
Loop
If Page.IsPostBack Then
SelectedAuthor.Text=Authors.SelectedItem.Text
End if
Catch ex As SqlException
Status.Text = ex.Message
Finally
conn.Close()
End Try
End Sub
</script>

<body>

<h3>Using a DataReader object to retrieve data</h3>
<form runat="server">
<asp:label id="Status" runat="server"/><br />
<asp:listbox id="Authors" rows=10 autopostback="true" runat="server"/><br /><br/>
Selected author:
<asp:label id="SelectedAuthor" runat="server"/>
</form>

</body>
</html>
 
Back
Top