请检查错误 [英] Please check the error

查看:97
本文介绍了请检查错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?

我无法显示数据。

请更正试用和捕获声明



我尝试过:



can anyone help me please?
I was unable to display the data.
please correct the try and catch statement

What I have tried:

private void btn_Save_Click(object sender, EventArgs e)
        {
            string date = dateTimePicker1.Text;
            string taskName = txt_TaskName.Text;
            string notes = txt_Notes.Text;

            if(txt_TaskName.Text!="" && txt_Notes.Text!="")
            {
                try
                {
                    String str = "Data Source=DESKTOP-CCDDCMC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    String query = "insert into tbl_Task(t_taskName,t_date,t_notes) values('" + taskName + "','" + date + "','" + notes + "')";
                    SqlConnection con = new SqlConnection(str);
                    SqlCommand cmd = new SqlCommand(query, con);

                    con.Open();
                    int result = cmd.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("Data Saved Successfully.....");
                        DisplayData();

                        txt_TaskName.Text = "";
                        txt_Notes.Text = "";
                        dateTimePicker1.Text = "";
                    }

                    con.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("error");
                }
                
            }
            else
            {
                MessageBox.Show("Please Provide Details!");
            }      
        }

推荐答案

引用:

不显示我在C#中使用数据库存储的数据

Not displaying data which I stored using database in C#

这里有2个问题。

要么ypu无法恢复数据,要么无法显示数据。

调试器将显示你的代码所做的事情,它将帮助你发现问题。



你应该学会尽快使用调试器尽可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

Here you 2 questions.
Either ypu fail to recover the data, or you fail to display the data.
The debugger will show what your code does, it will help you to spot the problem.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这是一个小的修复将显示错误是什么。

您只在消息框中显示错误一词

显示异常的错误消息。

但是,您需要了解调试器。



调试器是你拥有的最重要的工具之一。



Here's a small fix that will show what the error is.
You are only showing in the message box the word "error"
Show the error message from the exception.
However, you need to get to know your debugger.

The debugger is one of the most important tools you have.

try
{
    // your code here
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}


几点:

- 你没有处置Sql *对象,这应该在他们处理时完成不再需要

- 你不使用参数,所以你很容易受到SQL注入和类型转换问题的影响

- 你隐藏了实际的异常而只是显示出了点问题,改为显示实际原因



我建议通过正确执行数据库操作 [ ^ ]解决了这些问题。
Few points:
- You don't dispose Sql* objects, this should be done when they are no longer needed
- You don't use parameters, so you're vulnerable to SQL injection and type conversion problems
- you hide the actual exception and just show that something went wrong, instead show the actual reason

I suggest going through Properly executing database operations[^] in which these problems are addressed.


这篇关于请检查错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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