web 2.0

ASP.NET(vb.net) & SQL Server Check Exists Rows Data

ASP.NET(vb.net) & SQL Server Check Exists Rows Data - This is  example scripts how to use ASP.NET check exists  rows record in SQL Server table.

ShotDev Focus:
- ASP.NET(vb.net) & SQL Server Check Already Exists Add/Insert Record

Example

AspNetSQLServerExistsRecord.aspx

  1. <%@ Import Namespace="System.Data"%>  
  2. <%@ Import Namespace="System.Data.SqlClient"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Dim objConn As New SqlConnection  
  6. Dim objCmd As New SqlCommand  
  7. Dim strConnString,strSQL As String  
  8.   
  9. Sub Page_Load(sender As Object, e As EventArgs)  
  10. strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"  
  11. objConn.ConnectionString = strConnString  
  12. objConn.Open()  
  13. End Sub  
  14.   
  15. Sub btnSave_Click(sender As Object, e As EventArgs)  
  16. Dim intNumRows As Integer  
  17. strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"Me.txtCustomerID.Text &"' "  
  18. objCmd = New SqlCommand(strSQL, objConn)  
  19. intNumRows = objCmd.ExecuteScalar()  
  20.   
  21. IF intNumRows > 0 Then  
  22. Me.pnlAdd.Visible = False  
  23. Me.lblStatus.Visible = True  
  24. Me.lblStatus.Text = "CustomerID already exists."  
  25. Else  
  26.   
  27. strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _  
  28. " VALUES " & _  
  29. " ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _  
  30. " '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"  
  31.   
  32. objCmd = New SqlCommand  
  33. With objCmd  
  34. .Connection = objConn  
  35. .CommandText = strSQL  
  36. .CommandType = CommandType.Text  
  37. End With  
  38.   
  39. Me.pnlAdd.Visible = False  
  40. Try  
  41. objCmd.ExecuteNonQuery()  
  42. Me.lblStatus.Text = "Record Insert Sucessful."  
  43. Me.lblStatus.Visible = True  
  44. Catch ex As Exception  
  45. Me.lblStatus.Visible = True  
  46. Me.lblStatus.Text = "Record Cannot Insert : Error ("& ex.Message &")"  
  47. End Try  
  48.   
  49. End IF  
  50.   
  51. End Sub  
  52.   
  53. Sub Page_UnLoad()  
  54. objConn.Close()  
  55. objConn = Nothing  
  56. End Sub  
  57.   
  58. </script>  
  59. <html>  
  60. <head>  
  61. <title>ShotDev.Com Tutorial</title>  
  62. </head>  
  63. <body>  
  64. <form id="form1" runat="server">  
  65. <asp:Panel id="pnlAdd" runat="server">  
  66. <table width="353" border="1">  
  67. <tbody>  
  68. <tr>  
  69. <td width="102">  
  70. &nbsp;<asp:Label id="lblCustomerID" runat="server" text="CustomerID"></asp:Label></td>  
  71. <td width="235">  
  72. &nbsp;<asp:TextBox id="txtCustomerID" runat="server" Width="79px"></asp:TextBox>  
  73. </td>  
  74. </tr>  
  75. <tr>  
  76. <td>  
  77. &nbsp;<asp:Label id="lblName" runat="server" text="Name"></asp:Label></td>  
  78. <td>  
  79. &nbsp;<asp:TextBox id="txtName" runat="server" Width="177px"></asp:TextBox>  
  80. </td>  
  81. </tr>  
  82. <tr>  
  83. <td>  
  84. &nbsp;<asp:Label id="lblEmail" runat="server" text="Email"></asp:Label></td>  
  85. <td>  
  86. &nbsp;<asp:TextBox id="txtEmail" runat="server" Width="155px"></asp:TextBox>  
  87. </td>  
  88. </tr>  
  89. <tr>  
  90. <td>  
  91. &nbsp;<asp:Label id="lblCountryCode" runat="server" text="CountryCode"></asp:Label></td>  
  92. <td>  
  93. &nbsp;<asp:TextBox id="txtCountryCode" runat="server" Width="38px"></asp:TextBox>  
  94. </td>  
  95. </tr>  
  96. <tr>  
  97. <td>  
  98. &nbsp;<asp:Label id="lblBudget" runat="server" text="Budget"></asp:Label></td>  
  99. <td>  
  100. &nbsp;<asp:TextBox id="txtBudget" runat="server" Width="76px"></asp:TextBox>  
  101. </td>  
  102. </tr>  
  103. <tr>  
  104. <td>  
  105. &nbsp;<asp:Label id="lblUsed" runat="server" text="Used"></asp:Label></td>  
  106. <td>  
  107. &nbsp;<asp:TextBox id="txtUsed" runat="server" Width="76px"></asp:TextBox>  
  108. </td>  
  109. </tr>  
  110. </tbody>  
  111. </table>  
  112. <br />  
  113. <asp:Button id="btnSave" onclick="btnSave_Click" runat="server" Text="Save"></asp:Button>  
  114. <br />  
  115. </asp:Panel>  
  116. <asp:Label id="lblStatus" runat="server" visible="False"></asp:Label>  
  117. </form>  
  118. </body>  
  119. </html>  

Screenshot

ASP.NET(vb.net) & SQL Server Check Exists Rows Data

ASP.NET(vb.net) & SQL Server Check Exists Rows Data
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.