Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to show validation  using Stored procedure that if we typed invalid account no it should show validation message as invalid account no through sql server.And  where to give code in front end (vb code)


What I have tried:

VB code


Imports SMARTCORE_BL
Imports SMARTCORE_DA
Imports SMARTCORE_Common.CommonClass
Imports SMARTCORE_DAL
Imports SMARTCORE_BO
Imports SMARTCORE_UI

Public Class Learnnew
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            If Session("UserID") = "" Then
                Response.Redirect("~/Smartlogin.aspx")
                Exit Sub
            End If


            If Not IsPostBack Then
                Dim M_Comm As New CommonBL


            End If

               
        Catch ex As Exception
        End Try

    End Sub

    Protected Sub TxtAcc_TextChanged(sender As Object, e As EventArgs) Handles TxtAcc.TextChanged

        Try
            Dim M_CommonBL As New CommonBL
           
            Dim Dt_AcDtls As New DataTable
            Dt_AcDtls = M_CommonBL.samplelearn(TxtAcc.Text)
            TxtName.Text = Dt_AcDtls.Rows(0)("Cust_Name").ToString()
            TxtAddress.Text = Dt_AcDtls.Rows(0)("Add_Address1").ToString()

        Catch ex As Exception
            ShowAlertMessage(ex.Message)
        End Try


    End Sub


    Public Shared Sub ShowAlertMessage(ByVal [error] As String)
        Try
            Dim page As Page = TryCast(HttpContext.Current.Handler, Page)
            If page IsNot Nothing Then
                [error] = [error].Replace("'", "`")
                ScriptManager.RegisterStartupScript(page, page.[GetType](), "err_msg", "alert('" & [error] & "');", True)
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class





Class----Common BL
------------------------

Imports SMARTCORE_Common
Imports SMARTCORE_DA
Imports SMARTCORE_BO

Public Class CommonBL
Public Function samplelearn(ByVal Acc_No As String) As DataTable
        Try
            Dim M_DataTable As New DataTable
            Dim M_CommonDA As New CommonDA
            M_DataTable = M_CommonDA.samplelearn(Acc_No)
            Return M_DataTable
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End class




Class---- Common DA
-------------------------------

Imports SMARTCORE_DAL
Imports SMARTCORE_Common
Imports SMARTCORE_BO
Imports SMARTCORE_DA
Imports System.IO
Imports System.Data.OleDb
Imports System.Configuration
Imports Microsoft.SqlServer
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports SMARTCORE_BL

Public Class CommonDA

  Public Function samplelearn(ByVal Acc_No As String) As DataTable
        Try
            Dim M_DataTable As New DataTable
            Dim DtDeno As New DataTable
            Dim M_Sql As String
            Dim ClsComm As New CommonClass
            Dim con As New SqlConnection
            con = M_DBConn.GetConnection()
            Dim cmd As SqlCommand = New SqlCommand("Exec learnsample1 @Acc_No", con)

            Dim par1 As SqlParameter = New SqlParameter("@Acc_No", SqlDbType.VarChar, 20)

            par1.Value = Acc_No

            cmd.Parameters.Add(par1)

            Dim sdaa As New SqlDataAdapter(cmd)
            Dim Ds1 As New DataSet
            sdaa.Fill(Ds1)
            If Ds1.Tables.Count > 0 Then
                M_DataTable = Ds1.Tables(0)
            End If
            Return M_DataTable
        Catch ex As Exception

        End Try
    End Function

End Class





Stored Procedure
------------------


ALTER PROCEDURE [dbo].[validation_sp] (@Acc_No varchar(20))
AS
BEGIN
    IF NOT EXISTS (SELECT * FROM AccMain_M_Tbl WHERE Acc_No = @Acc_No)
    BEGIN
        SELECT 'Invalid Account No' AS ErrorMsg
        RETURN
    END

    SELECT 
        Cust_Name, 
        Add_Address1 
    FROM 
        AccMain_M_Tbl a 
        INNER JOIN Cust_Master_M_Tbl b ON a.ACC_CustID = b.Cust_Id
        INNER JOIN Cust_Address_M_Tbl c ON b.Cust_Id =c.Cust_Id 
    WHERE 
        Acc_No = @Acc_No
END
Posted
Updated 25-Mar-23 4:57am

1 solution

There are three ways to pass info back from a stored procedure - and all of them require code on your side to actual reflect that to your user - simply because SQL Server is a database which has no direct contact with users at all. In your case it's even worse, because your user is potentially several thousand miles away using a mobile phone instead of a computer!

The first way is via a return value: a single (normally integer) value your code can check. Multiple values are a problem, so other than a Success/Error code, there isn't a lot you can do.
The second is via OUTPUT parameters: this lets you return more info if you need to.
The final way is by throwing an error: this means your code has to catch the exception and since your existing code seems to swallow exceptions instead of handling them it's probably not for you...

Whichever way you chose, your code has to recognise the problem an d translate that into something the user can both see, and understand.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900