-
January 16th, 2012, 06:43 PM
#1
Need help binding a dataview from a textbox
I have been battling with this for WAY too long for how simple it should be lol
I am trying to create a basic webpage that will check a column of a database table to see if a "UPC Code" is taken already, to prevent duplicate use. I'm fairly new at .NET programming, but have built asp pages before. I'm just stumped :/
I have a textbox that a user can type in a number. The page successfully checks to see if the textbox is blank and reports the error, and if there is a value, it's supposed to bind that textbox number to a gridview to see which Item# it's associated with. But for some reason, my code is not binding the value of the textbox to the gridview.
Code:
<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeBehind="Default.aspx.vb" Inherits="UPC_Checker._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
UPC Code Checker
</h2>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FSDBGLConnectionString %>"
SelectCommand="SELECT [ItemNumber], [ItemUPC] FROM [_NoLock_FS_Item] WHERE ([ItemUPC] = @ItemUPC)">
<SelectParameters>
<asp:SessionParameter Name="ItemUPC" SessionField="UPCText.Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:TextBox ID="UPCText" runat="server" MaxLength="12"></asp:TextBox>
<asp:Button ID="CheckUPC" runat="server" Text="Check Availability" onclick="CheckUPC_Click" autopostback="true" />
</p>
<asp:Label ID="lblErrorMessage" runat="server" ForeColor="Red"></asp:Label>
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ItemNumber" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ItemNumber" HeaderText="ItemNumber" ReadOnly="True"
SortExpression="ItemNumber" />
<asp:BoundField DataField="ItemUPC" HeaderText="ItemUPC"
SortExpression="ItemUPC" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</asp:Content>
Code Behind:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Data.SqlClient
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("FSDBGLConnectionString").ConnectionString)
Dim cmd As SqlCommand = con.CreateCommand()
If IsPostBack Then
con.Open()
cmd.CommandText = "SELECT [ItemNumber], [ItemUPC] FROM [_NoLock_FS_Item] WHERE ([ItemUPC] = '" & UPCText.Text & "')"
cmd.ExecuteNonQuery()
GridView1.DataBind()
cmd.Dispose()
con.Close()
End If
'End If
End Sub
Sub CheckUPC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckUPC.Click
If UPCText.Text = "" Then
lblErrorMessage.Text = "Please enter a UPC Code to check."
End If
End Sub
End Class
-
January 16th, 2012, 09:22 PM
#2
Re: Need help binding a dataview from a textbox
-
January 17th, 2012, 11:08 AM
#3
Re: Need help binding a dataview from a textbox
So that link goes to MSDN site with the search term "migrated"... not sure how that applies here :\
-
January 17th, 2012, 01:27 PM
#4
Re: Need help binding a dataview from a textbox
OK, they've changed the site a bit: http://code.msdn.microsoft.com/
Pick your environment
-
January 20th, 2012, 01:42 PM
#5
Re: Need help binding a dataview from a textbox
I have this fixed now, it was a small thing I was missing in my code behind (i knew it was going to be a simple thing...
Here's the changes I made to get it working properly:
cmd.CommandText = "SELECT [ItemNumber], [ItemUPC] FROM [_NoLock_FS_Item] WHERE ([ItemUPC] = @UPC)"
cmd.Parameters.AddWithValue("@UPC", UPCText.Text)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|