Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
here am developing one task please read and tell me the modifications

here once enter the number and after press button1(checknumber) check in database this number is there or not and once is tnere in database that related column amount displayed. once is not there automatically will come on this number not valid this message i want please reply me here i finished amount displying but error popup(number not exist) this message i want please reply me
C#
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
        try
        {
            con.Open();
            SqlCommand cmd2 = new SqlCommand("select Numbers.amount from Numbers inner join submitTable on Numbers.Number=submitTable.number where Numbers.Number=" + txtdsp.Text, con);
            lbldisplay.Text = cmd2.ExecuteScalar().ToString();
            con.Close();
        }
        catch
        {
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int userId = 0;
        string constr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SP_SubmitTable"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Number", txtdsp.Text.Trim());
                    cmd.Parameters.AddWithValue("@Amount", lbldisplay.Text);
                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
            }
            string message = string.Empty;
            switch (userId)
            {
                case -1:
                    message = "Already been used this number code.";
                    break;
                default:
                    message = "Submited Successful.";
                    break;
            }
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }
}
Posted
Updated 25-Aug-14 23:12pm
v2
Comments
David Lee 145 26-Aug-14 5:26am    
Did you check the stored procedure "SP_SubmitTable" makes a right result?
member1431 26-Aug-14 5:36am    
Dear David,
please check this stored procedure:

USE [Practice]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_Submittable]

@Number nvarchar(max)=null,
@Amount nvarchar(max)=null
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT Id FROM Submittable WHERE Number= @Number)
BEGIN
SELECT -1 -- Voucher number Already exists.
END

BEGIN

INSERT INTO [Submittable]
([Number],
[Amount]
)
VALUES
(@Number,
@Amount
)
SELECT SCOPE_IDENTITY() -- Id
END
END


check the "else" in the middle of your if statement.

SQL
USE [Practice]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
ALTER PROCEDURE [dbo].[SP_Submittable]
 
@Number nvarchar(max)=null,
@Amount nvarchar(max)=null
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT Id FROM Submittable WHERE Number= @Number)
BEGIN
SELECT -1 -- Voucher number Already exists.
END
-- Check here
ELSE
BEGIN

INSERT INTO [Submittable]
([Number],
[Amount]
)
VALUES
(@Number,
@Amount
)	
SELECT SCOPE_IDENTITY() -- Id	
END
END
 
Share this answer
 
Don't use connection string on every button click event, its not good practice.

I guess you want something like this:

C#
SqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.Read())
{
   //Do the operation..
}
else
{
   MessageBox.Show("number not exist");
}
 
Share this answer
 
v4
Comments
[no name] 26-Aug-14 7:08am    
Showing a messagebox on a website? What makes you think that will work?

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