Display (Show) message if user does NOT exist in Database using C# and VB.Net in ASP.Net

kankon
 
on Nov 22, 2022 09:50 PM
330 Views

hello,

I have a problem with update user info, when i try update user info and if i add in his name like kankon, i change name to kankons then i press update its add me

Do i need add something else?

i want add a message u must register first or there is no name for kankons in data please contact Admin

SQL

USE [cm]
GO
/****** Object:  StoredProcedure [dbo].[update_User_C3]    Script Date: 22/11/2022 10:07:28 م ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[update_UserS]
(
    
     @name NVARCHAR(MAX)=NULL,
	 @fileid NVARCHAR(MAX)=NULL,
	 @civilid NVARCHAR(MAX)=NULL,
	 @Countries NVARCHAR(MAX)=NULL,
	 @States NVARCHAR(MAX)=NULL,
	 @Cities NVARCHAR(MAX)=NULL,
   @mobile NVARCHAR(MAX)=NULL,
    @jop NVARCHAR(MAX)=NULL

	)
	AS
       BEGIN
      SET NOCOUNT ON;
      IF EXISTS(SELECT id FROM Table_infoname WHERE name = @name)
      BEGIN
            SELECT -1 -- name exists.
      END
	  ELSE IF EXISTS(SELECT id FROM Table_infoname WHERE civilid = @civilid)
      BEGIN
            SELECT -2 -- civilid exists.
     END
	  ELSE IF EXISTS(SELECT id FROM Table_infoname WHERE fileid = @fileid)
      BEGIN
            SELECT -3 -- fileid exists.
     END
      ELSE
      BEGIN
	  UPDATE Table_infoname 
	 SET
     
      name=@name,
	  fileid=@fileid,
      civilid=@civilid,

	   Countries=@Countries,
	    States=@States,
		 Cities=@Cities,
		 jop=@jop,
		 mobile=@mobile
		 

		 where civilid=@civilid

	  end
	
	END

code behind c#

protected void Button28_Click(object sender, EventArgs e)
{
    int Id = 0;
    string constr = ConfigurationManager.ConnectionStrings["cmConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("update_UserS"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;
 
                if (!string.IsNullOrEmpty(fName.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@name", fName.Text.ToString());
                }
 
                if (!string.IsNullOrEmpty(ffileid.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@fileid", ffileid.Text.ToString());
                }
 
                if (!string.IsNullOrEmpty(fcivilid.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@civilid", fcivilid.Text.ToString());
                }
 
                if (!string.IsNullOrEmpty(mobile.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@mobile", mobile.Text.ToString());
                }
                if (!string.IsNullOrEmpty(joptitel.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@jop", joptitel.Text.Trim());
                }                      
 
                if (!string.IsNullOrEmpty(ddlCountries2.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@Countries", ddlCountries2.Text.ToString());
                }
                if (!string.IsNullOrEmpty(ddlStates2.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@States", ddlStates2.Text.ToString());
                }
                if (!string.IsNullOrEmpty(ddlCities2.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@Cities", ddlCities2.Text.ToString());
                }
 
                cmd.Connection = con;
                con.Open();
                Id = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
            string message = string.Empty;
            switch (Id)
            {
                case -1:
                    message = "اسم المستخدم موجود مسبقا";
                    break;
 
                case -2:
                    message = "الرقم المدني مستخدم";
                    break;
                    case -3:
 
                    message = "رقم الملف مستخدم";
                    break;
 
                default:
                    message = "تم تحديث بيانات الموظف بنجاح\\nاسم الموظف : " + fName.Text.ToString();
                    
                    break;
            }
 
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Nov 23, 2022 06:39 AM
on Nov 23, 2022 06:41 AM

Hi kankon,

Please refer below sample.

HTML

<table>
    <tr>
        <td>Name: </td>
        <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="OnSubmit" /></td>
    </tr>
</table>

Namespaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Code

C#

protected void OnSubmit(object sender, EventArgs e)
{
    int Id = 0;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("update_UserS", con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;

                if (!string.IsNullOrEmpty(txtName.Text.Trim()))
                {
                    cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString());
                }

                cmd.Connection = con;
                con.Open();
                Id = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
            string message = string.Empty;
            switch (Id)
            {
                case -1:
                    message = "User Name is already Exists.";
                    break;

                default:
                    message = "You must register first.\\n Name: " + txtName.Text.ToString();

                    break;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }
}

VB.Net

Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
    Dim Id As Integer = 0
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand("update_UserS", con)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.CommandType = CommandType.StoredProcedure

                If Not String.IsNullOrEmpty(txtName.Text.Trim()) Then
                    cmd.Parameters.AddWithValue("@Name", txtName.Text.ToString())
                End If

                cmd.Connection = con
                con.Open()
                Id = Convert.ToInt32(cmd.ExecuteScalar())
                con.Close()
            End Using
            Dim message As String = String.Empty
            Select Case Id
                Case -1
                    message = "User Name is already Exists."
                Case Else
                    message = "You must register first.\n Name: " & txtName.Text.ToString()
            End Select
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" & message & "');", True)
        End Using
    End Using
End Sub

Screenshot