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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  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. Similar Questions and Discussions

    1. Determine Selected Cell in Gridview
      Hi all, I have created a row selectable gridview control, however I am looking to add a little more functionality. Currently, I can select a row...
    2. Newbie, add a button to gridview footer
      Hi Can anyone help me or point me in the right direction, I'm running Win XP Pro SP2, VS2005 Team, .Net Framework 2.0 and SQL Server2005. I...
    3. Newbie, adding a row to Gridview in VB
      Hi Can anyone help me or point me in the right direction, I'm running Win XP Pro SP2, VS2005 Team, .Net Framework 2.0 and SQL Server2005. I...
    4. Newbie, gridview problem Asp.net 2005
      Hi Can anyone help me I'm running Win XP Pro SP2, VS2005 Team, .Net Framework 2.0 and SQL Server2005, I can't delete records using the gridviews...
    5. Cell handling when databinding in a GridView
      I have a table with some url's (e.g. MyLinks.) In the database, the table has a field "AllowDelete". The grid is set up with two columns. One...
  3. #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

  4. #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

Posting Permissions

  • You may not post new threads
  • You may 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