I am having some trouble pulling the value of a select statement in the aspx page to insert on my vb page as illustrate below.
ASPX Page:
<asp:SqlDataSource ID="Price" runat="server"
SelectCommand="SELECT Price FROM Order WHERE ViewedFileName = @ViewedFileName"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>">
<SelectParameters>
<asp:ControlParameter ControlID="Item" Name="ViewedFileName" PropertyName="Value" DefaultValue="0.00"/>
</SelectParameters>
</asp:SqlDataSource>
VB Code:
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Orders(Price)" & " VALUES (@Price)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Parameters.AddWithValue("Price", Not Sure What To Do Here)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Dim selPrice As Decimal
Item.Value = "SomeViewedFileName"
Using conn As New SqlConnection(Price.connectionstring)
Using cmd1 As New SqlCommand(Price.SelectCommand, conn)
conn.Open()
Dim dReader As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection)
If dReader.HasRows Then
dReader.Read()
selPrice = dReader(0)
End If
' selPrice = ExecuteScalar instead of the dReader
End Using
End Using
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Orders(Price) VALUES (@Price)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Parameters.AddWithValue("Price", selPrice)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
If you are always getting a single value, I would use ExecuteScalar, instead of the ExecuteReader.
jthomas8946
Member
181 Points
240 Posts
Add value in vb page insert from .aspx select statement
Feb 25, 2013 11:15 PM|LINK
I am having some trouble pulling the value of a select statement in the aspx page to insert on my vb page as illustrate below.
ASPX Page:
<asp:SqlDataSource ID="Price" runat="server" SelectCommand="SELECT Price FROM Order WHERE ViewedFileName = @ViewedFileName" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"> <SelectParameters> <asp:ControlParameter ControlID="Item" Name="ViewedFileName" PropertyName="Value" DefaultValue="0.00"/> </SelectParameters> </asp:SqlDataSource>VB Code:
Dim cmd As New SqlCommand() cmd.CommandText = "INSERT INTO Orders(Price)" & " VALUES (@Price)" cmd.CommandType = CommandType.Text cmd.Connection = con cmd.Parameters.AddWithValue("Price", Not Sure What To Do Here) con.Open() cmd.ExecuteNonQuery() con.Close()I appreciate any help offered!
Thanks!
oned_gk
All-Star
31651 Points
6468 Posts
Re: Add value in vb page insert from .aspx select statement
Feb 25, 2013 11:33 PM|LINK
Getting price
Inserting price, set insertcommand and parameter to sqldatasource
<asp:SqlDataSource ID="Price" runat="server" SelectCommand="SELECT Price FROM Order WHERE ViewedFileName = @ViewedFileName" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" InsertCommand="INSERT INTO Orders (Price) VALUES (@Price)"> <SelectParameters> <asp:ControlParameter ControlID="Item" Name="ViewedFileName" PropertyName="Value" DefaultValue="0.00"/> </SelectParameters> <InsertParameters> <asp:Parameter Name="Price" /> </InsertParameters> </asp:SqlDataSource>Price.InsertParameters("Price").DefaultValue = pricevalue Price.Insert()jthomas8946
Member
181 Points
240 Posts
Re: Add value in vb page insert from .aspx select statement
Feb 26, 2013 01:57 AM|LINK
Oned_GK,
Thanks for your help. I attempted your code and it does not produce any error, but it is submitting 0 instead of the actual price. Thoughts?
Thanks!
paindaasp
Star
12090 Points
2034 Posts
Re: Add value in vb page insert from .aspx select statement
Feb 26, 2013 04:32 AM|LINK
This should be close:
Dim selPrice As Decimal Item.Value = "SomeViewedFileName" Using conn As New SqlConnection(Price.connectionstring) Using cmd1 As New SqlCommand(Price.SelectCommand, conn) conn.Open() Dim dReader As SqlDataReader = cmd1.ExecuteReader(CommandBehavior.CloseConnection) If dReader.HasRows Then dReader.Read() selPrice = dReader(0) End If ' selPrice = ExecuteScalar instead of the dReader End Using End Using Dim cmd As New SqlCommand() cmd.CommandText = "INSERT INTO Orders(Price) VALUES (@Price)" cmd.CommandType = CommandType.Text cmd.Connection = con cmd.Parameters.AddWithValue("Price", selPrice) con.Open() cmd.ExecuteNonQuery() con.Close()If you are always getting a single value, I would use ExecuteScalar, instead of the ExecuteReader.
oned_gk
All-Star
31651 Points
6468 Posts
Re: Add value in vb page insert from .aspx select statement
Feb 26, 2013 07:54 AM|LINK
check dt.Rows(0).Item(0) value, use debug or label text.
Maybe problem from your "Item" control. make sure ViewedFileName get right value.