注册 登录
编程论坛 C# 论坛

MessageBox.Show(string)不能显示

ybh24138227 发布于 2015-01-09 10:05, 4862 次点击
  string a=textBox1.Text;
            string b=textBox2.Text;
            if (a == "" || b == "")
            {
                MessageBox.Show("账号或密码不能为空!","提示");
            }
            else
            {
                string ssql = "insert into chaoshi_users(id,name,pwd) values('" + textBox1.Text + "','" + textBox2.Text + "','0')";
                SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
                cn.Open();
                SqlCommand cmd = new SqlCommand(ssql, cn);
                cmd.ExecuteNonQuery();

                cn.Close();
                MessageBox.Show("注册成功!", "提示");



代码如上,可是运行时到了MessageBox哪里整个程序都没有反应了,提示信息也不显示,求解决!
9 回复
#2
wp2319572015-01-09 10:09
我这里没问题

 private void button1_Click(object sender, EventArgs e)
        {
            string a = textBox1.Text;
            string b = textBox2.Text;
            if (a == "" || b == "")
            {
                MessageBox.Show("账号或密码不能为空!", "提示");
            }
        }
当然,测试前提是 2个text需要有一个为空
#3
over12302015-01-09 10:40
感觉你是SQL语句有问题:
string ssql = "insert into chaoshi_users(id,name,pwd) values('" + textBox1.Text + "','" + textBox2.Text + "','0')";

表名要加 [],
id是什么格式的?int还是string ?
按你的情况,前面提示的两个text是 name 和pwd , 但存的却是 id 和 name
#4
ybh241382272015-01-09 10:40
回复 2楼 wp231957
我的是有空值的,好像记得昨天下午有一次测试没问题,其他的时候都不行,就像无响应一样,测试的其他窗体也都不能动
#5
ybh241382272015-01-09 10:44
回复 3楼 over1230
插入的不是空值时数据库确实插进数据了,但是C#窗体就是没有反应,MessageBox也不弹出来。额,那些列名我随便写的
#6
over12302015-01-09 10:47
会不会是你的textBox里面有空格:
            string a = textBox1.Text;
            string b = textBox2.Text;
            if ( a.Trim () == "" || b.Trim () == "")
            {
                MessageBox.Show("账号或密码不能为空!", "提示");
            }
这样试下
#7
ybh241382272015-01-09 10:52
回复 6楼 over1230
还是不行,和刚才那种一样
#8
chenqi56242015-01-09 22:13
try
{
    string ssql = "insert into chaoshi_users(id,name,pwd) values('" + textBox1.Text + "','" + textBox2.Text + "','0')";
                SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
                cn.Open();
                SqlCommand cmd = new SqlCommand(ssql, cn);
                cmd.ExecuteNonQuery();

                cn.Close();
                MessageBox.Show("注册成功!", "提示");
}
catch(exception ex)
{
Messagebox.show(ex.message);
}代码没看 先不管先看异常
#9
ybh241382272015-01-10 20:39
回复 8楼 chenqi5624
这个怎么调试啊?为啥我把代码输进一个新的C#文件不能调试出结果来呢?
#10
Annaro2015-02-16 22:37
你的代码有问题
1.if (a == "") ,最好写成 if (string.IsNullOrEmpty(a))
2. string ssql = "insert into chaoshi_users(id,name,pwd) values('" + textBox1.Text + "','" + textBox2.Text + "','0')";
最好用string.Format();
3.SqlConnection  应该全局创建好,要用的时候判断下,不要判断再建。
4. cmd.ExecuteNonQuery();加上try catch
1