Display dataset results in textboxes

G

Guest

Hello -

I have the following stored procedure and code. I want to put the results
in two textboxes. I get to the part where I create the dataset and then I
don't know what to do. I tried using a dataview with no luck.

Stored procedure:
ALTER Proc spFillSbjRply
(
@key int
)
AS
Select
tblLogin.ScreenName, tblPost.Question
from tblPost
inner join tblLogin
ON tblLogin.UserID = tblPost.UserID
Where tblPost.PostID=@key

Click event of a button:
index = dgPost.SelectedIndex
key = dgPost.DataKeys(index)
Dim dsSbjRply as New DataSet
Dim cmdSbjRply as New SqlClient.SqlCommand()
Dim daSbjRply as New SqlClient.SqlDataAdapter()
Dim prmSbjRply as SqlClient.SqlParameter

cmdSbjRply = cnn.CreateCommand
cmdSbjRply.CommandType = CommandType.StoredProcedure
cmdSbjRply.CommandText = "spFillSbjRply"
prmSbjRply = cmdSbjRply.Parameters.Add("@key", SqlDbType.Int, 4)
prmSbjRply.Value = key

daSbjRply.SelectCommand = cmdSbjRply
daSbjRply.Fill(dsSbjRply, "PostQ")

I'M STUCK HERE.

Any help will be immensely appreciated!!
 
K

Ken Cox [Microsoft MVP]

Hi Sandy,

I'm not sure what's inside your dataset, but here's some example code that
might get you unstuck. You need to use a dataview and a rowfilter
expression.

Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset plus an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
 
G

Guest

Hi Ken -

Thanks for your reply! Wow -- seems like a lot of code for little 'ol
textboxes.

My stored procedure results, hence my dataset, consist of two items -
ScreenName and Question. The "key" is what filters it into one row. Since I
am already "filtered," how does this part work:

ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"

I can get both items to show up in a repeater if I don't use a stored
procedure and just put the Select statement in code (which I DON'T want to
do). Also, I don't want "ScreenName" in the repeater because I want to reuse
it for an insert and I don't know how to get that value out of a repeater . .
.. that's why I'm opting for textboxes now . . . it's easy to get the values
out.

Sorry I wasn't clear -- don't know how clear the above is either . . . Any
ideas of a shorter method? I have a gazillion things going on in this page
and was hoping for simple code for this, if possible. I'm easily confused.

Thanks again, Ken

Sandy

Ken Cox said:
Hi Sandy,

I'm not sure what's inside your dataset, but here's some example code that
might get you unstuck. You need to use a dataview and a rowfilter
expression.

Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset plus an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function

Sandy said:
Hello -

I have the following stored procedure and code. I want to put the results
in two textboxes. I get to the part where I create the dataset and then I
don't know what to do. I tried using a dataview with no luck.

Stored procedure:
ALTER Proc spFillSbjRply
(
@key int
)
AS
Select
tblLogin.ScreenName, tblPost.Question
from tblPost
inner join tblLogin
ON tblLogin.UserID = tblPost.UserID
Where tblPost.PostID=@key

Click event of a button:
index = dgPost.SelectedIndex
key = dgPost.DataKeys(index)
Dim dsSbjRply as New DataSet
Dim cmdSbjRply as New SqlClient.SqlCommand()
Dim daSbjRply as New SqlClient.SqlDataAdapter()
Dim prmSbjRply as SqlClient.SqlParameter

cmdSbjRply = cnn.CreateCommand
cmdSbjRply.CommandType = CommandType.StoredProcedure
cmdSbjRply.CommandText = "spFillSbjRply"
prmSbjRply = cmdSbjRply.Parameters.Add("@key", SqlDbType.Int, 4)
prmSbjRply.Value = key

daSbjRply.SelectCommand = cmdSbjRply
daSbjRply.Fill(dsSbjRply, "PostQ")

I'M STUCK HERE.

Any help will be immensely appreciated!!
 
Top