Need help binding a dataview from a textbox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Need help binding a dataview from a textbox

  1. #1
    Join Date
    Jan 2012
    Posts
    3

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Need help binding a dataview from a textbox

    This might help: link
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2012
    Posts
    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 :\

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jan 2012
    Posts
    3

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center