使用asp.net和sql显示错误消息? [英] display error message using asp.net and sql?

查看:77
本文介绍了使用asp.net和sql显示错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
这里正在制定一项任务,请阅读并告诉我所做的修改

在这里输入一次数字,然后按button1(checknumber)在数据库中检查该数字是否存在,并且在数据库中一次显示了相关列数.一次不存在,此号码将自动出现无效此消息我要在这里答复我我已完成金额显示,但错误弹出窗口(数字不存在)我要此消息请给我答复

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

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);
        }
    }
}

推荐答案

检查if语句中间的"else".

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

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


不要在每个按钮单击事件上使用连接字符串,这不是一个好习惯.

我想你想要这样的东西:

Don''t use connection string on every button click event, its not good practice.

I guess you want something like this:

SqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.Read())
{
   //Do the operation..
}
else
{
   MessageBox.Show("number not exist");
}


这篇关于使用asp.net和sql显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆