Professional Web Applications Themes

Newbie, how to check if a value in a gridview cell already exist in database - ASP.NET Building Controls

Hi I've the following dataset bound to my gridview <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NwindConnectionString %>" SelectCommand="SELECT p.ProductID, p.ProductName, p.CategoryID, c.CategoryName FROM Products p inner join Categories c on p.CategoryID = c.CategoryID" UpdateCommand="Update Products Set ProductID=ProductID, ProductName=ProductName, CategoryID=CategoryID where ProductID=ProductID" deletecommand="delete from products where productid=productid"> <DeleteParameters> <asp:Parameter Name="productID" Type="Int32" /> </DeleteParameters> </asp:SqlDataSource> How can check if a value in a gridview cell already exist in the database?, I use the following to check if the value entered for "ProductName" is null, but I also want to check if the value entered does in fact already exist in my database. Protected Sub ...

  1. #1

    Default Newbie, how to check if a value in a gridview cell already exist in database

    Hi

    I've the following dataset bound to my gridview
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NwindConnectionString %>"

    SelectCommand="SELECT p.ProductID, p.ProductName, p.CategoryID, c.CategoryName FROM Products p inner join Categories c on p.CategoryID = c.CategoryID"

    UpdateCommand="Update Products Set ProductID=ProductID, ProductName=ProductName, CategoryID=CategoryID where ProductID=ProductID"

    deletecommand="delete from products where productid=productid">

    <DeleteParameters>

    <asp:Parameter Name="productID" Type="Int32" />

    </DeleteParameters>

    </asp:SqlDataSource>

    How can check if a value in a gridview cell already exist in the database?, I use the following to check if the value entered for "ProductName" is null, but I also want to check if the value entered does in fact already exist in my database.

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    If e.NewValues("ProductName") = "" Then

    e.Cancel = True

    Return

    End If

    End Sub

    Thanks and best regards.



    verci Guest

  2. #2

    Post Re: Newbie, how to check if a value in a gridview cell already exist in database

    To check whether data in a cell already exists or not
    we have to check the complete column
    for that we have to write select query which checks for data to be entered already
    exists or not
    if it exists we have to come out of program
    else continue coding such as updating.

    for checking of existing data we use 'hasrows' fn in vb.net
    code:

    Dim CMD As New OleDbCommand
    CMD.CommandText = "SELECT * FROM tablename WHERE D_NAME='" & txtname.Text & "' AND CPD_PK_CODE <> '" & txtpkcode.Text & "'"
    dReader = CMD.ExecuteReader
    If dReader.HasRows Then
    lblmessage.Text = txtname.Text + "- already exists"
    Exit Sub
    Else
    CMD = New OleDbCommand
    CMD.Connection = con
    CMD.CommandText = "UPDATE tablename SET values"
    CMD.ExecuteNonQuery()

    here we are checking data which is entered from textboxes
    d_name is the column name which we have to check and pk_code is primary key column name
    anu Guest

  3. #3

    Default Re: Newbie, how to check if a value in a gridview cell already exist in database

    To check whether data in a cell already exists or not
    we have to check the complete column
    for that we have to write select query which checks for data to be entered already
    exists or not
    if it exists we have to come out of program
    else continue coding such as updating.

    for checking of existing data we use 'hasrows' fn in vb.net
    code:

    Dim CMD As New OleDbCommand
    CMD.CommandText = "SELECT * FROM tablename WHERE D_NAME='" & txtname.Text & "' AND CPD_PK_CODE <> '" & txtpkcode.Text & "'"
    dReader = CMD.ExecuteReader
    If dReader.HasRows Then
    lblmessage.Text = txtname.Text + "- already exists"
    Exit Sub
    Else
    CMD = New OleDbCommand
    CMD.Connection = con
    CMD.CommandText = "UPDATE tablename SET values"
    CMD.ExecuteNonQuery()

    here we are checking data which is entered from textboxes
    d_name is the column name which we have to check and pk_code is primary key column name
    alochana9 is offline Junior Member
    Join Date
    May 2011
    Location
    hyderabad
    Posts
    4

Similar Threads

  1. Replies: 2
    Last Post: April 26th, 05:58 AM
  2. Newbie, adding a row to Gridview in VB
    By verci in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 28th, 06:46 AM
  3. Newbie, gridview problem Asp.net 2005
    By verci in forum ASP.NET Building Controls
    Replies: 2
    Last Post: August 28th, 06:36 AM
  4. Cell handling when databinding in a GridView
    By HEM in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: August 23rd, 01:33 PM
  5. Replies: 3
    Last Post: September 18th, 07:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139