GridView RowUpdating无法使用ExecuteNonQuery() - 需要帮助 [英] GridView RowUpdating not working with ExecuteNonQuery() - need help

查看:65
本文介绍了GridView RowUpdating无法使用ExecuteNonQuery() - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行编辑,更新和删除操作的GridView。我在EditItemTemplate中有Textboxes。编辑工作正常,但事件GridView1_RowUpdating为executiontenonquery返回零。



这是我的存储过程



I am having a GridView which performs Edit,Update and Delete opearitons. I have Textboxes in EditItemTemplate. The Edit is working properly but the event GridView1_RowUpdating returns zero for executenonquery.

Here is my storedprocedure

ALTER PROC Reg_UpdateProc    
@COUNTRY VARCHAR(MAX),    
@TITLE VARCHAR(MAX),    
@FIRSTNAME VARCHAR(MAX),    
@LASTNAME VARCHAR(MAX),    
@CITY VARCHAR(MAX),    
@EMPADDRESS VARCHAR(MAX),    
@ZIP VARCHAR(MAX),    
@EMPSTATE VARCHAR(MAX),    
@PHONE VARCHAR(MAX),    
@TOPIC VARCHAR(MAX),    
@COMMENTS VARCHAR(MAX),
@EMAIL VARCHAR(MAX)    
as    
BEGIN    
UPDATE RegisterTable set country=@COUNTRY,Title=@TITLE,firstname=@FIRSTNAME,lastname=@LASTNAME,    
empaddress=@EMPADDRESS,zip=@ZIP,empstate=@EMPSTATE,phone=@PHONE,topic=@TOPIC,    
comments=@COMMENTS WHERE email=@EMAIL    
END



和GridView1_RowUpdating上的Gridview代码




and my Gridview code on GridView1_RowUpdating

protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection(Connection);
        con.Open();
        SqlCommand cmd = new SqlCommand("Reg_UpdateProc", con);
        Label country = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
        TextBox txtcountry = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        Label title = (Label)GridView1.Rows[e.RowIndex].FindControl("Label2");
        TextBox txttitle = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        Label firstname = (Label)GridView1.Rows[e.RowIndex].FindControl("Label3");
        TextBox txtfirstname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        Label lastname = (Label)GridView1.Rows[e.RowIndex].FindControl("Label4");
        TextBox txtlastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        Label empaddress = (Label)GridView1.Rows[e.RowIndex].FindControl("Label5");
        TextBox txtempaddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
        Label city = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
        TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
        Label zip = (Label)GridView1.Rows[e.RowIndex].FindControl("Label7");
        TextBox txtzip = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
        Label empstate = (Label)GridView1.Rows[e.RowIndex].FindControl("Label8");
        TextBox txtempstate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");
        Label phone = (Label)GridView1.Rows[e.RowIndex].FindControl("Label9");
        TextBox txtphone = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9");
        Label email = (Label)GridView1.Rows[e.RowIndex].FindControl("Label10");
        TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox13");
        Label topic = (Label)GridView1.Rows[e.RowIndex].FindControl("Label11");
        TextBox txttopic = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox11");
        Label comments = (Label)GridView1.Rows[e.RowIndex].FindControl("Label12");
        TextBox txtcomments = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox12");
        cmd.CommandType = CommandType.StoredProcedure;   
        cmd.Parameters.AddWithValue("@COUNTRY", txtcountry.ToString());
        cmd.Parameters.AddWithValue("@TITLE", txttitle.ToString());
        cmd.Parameters.AddWithValue("@FIRSTNAME", txtfirstname.ToString());
        cmd.Parameters.AddWithValue("@LASTNAME", txtlastname.ToString());
        cmd.Parameters.AddWithValue("@EMPADDRESS", txtempaddress.ToString());
        cmd.Parameters.AddWithValue("@CITY", txtcity.ToString());
        cmd.Parameters.AddWithValue("@ZIP", txtzip.ToString());
        cmd.Parameters.AddWithValue("@EMPSTATE", txtempstate.ToString());
        cmd.Parameters.AddWithValue("@PHONE", txtphone.ToString());
        cmd.Parameters.AddWithValue("@TOPIC", txttopic.ToString());
        cmd.Parameters.AddWithValue("@COMMENTS", txtcomments.ToString());
        cmd.Parameters.AddWithValue("@EMAIL", txtemail.ToString());
     
        int ret = cmd.ExecuteNonQuery();
        if (ret == 1)
        {
            TextBoxCity.Text = "okay";
        }
        else
        {
            Response.Write("not done");
        }
        con.Close();
    }
}





我得到的结果是没有完成。



请帮忙。在此先感谢。



The result I get is "not done."

Kindly help. Thanks in advance.

推荐答案

嗯,这里有很多错误,在这里出错很多。



你的代码很糟糕。您尚未逻辑命名控件。你怎么能确定你做对了,textbox13是电子邮件而不是电话号码?此代码不可读且不可维护。



您不应该在表示层中创建连接,您应该使用数据层,ONE方法应该返回连接。您应该了解连接池如何适用于您正在使用的版本,并使用它。您当然应该使用块来处理任何具有IDisposable的内容。



您应该匹配除电子邮件地址之外的其他内容。您的数据库是否强制执行仅存在于一行中的电子邮件?



您知道如何使用调试器吗?您确定数据没有更新吗?如果你获得了传入proc的值,并在SQL Server中调用proc,该怎么办?你有没有检查过电子邮件的价值是什么?可能的是,它不匹配(领先的空间,也许?)。



*叹*你是随机写这段代码的吗?在这里打破了这么多,我花了一段时间来发现问题。



Well, there's so much wrong here, and so much to go wrong here.

Your code is awful. You have not named your controls logically. How can you be sure you got it right and that textbox13 is the email and not the phone number ? This code is unreadable and unmaintainable.

You should not be creating connections in the presentation layer, you should use a data layer and ONE method should return connections. You should read up on how connection pools work for the versions you're using, and work with it. You should certainly use using blocks for anything that has IDisposable.

You should be matching on something other than an email address. Does your DB enforce an email only existing in one row ?

Do you know how to use your debugger ? Are you certain the data is not being updated ? What if you get the values you're passing in to the proc, and call the proc in SQL Server ? Have you checked at all what the email value is, going in ? Odds are that it's not matching ( leading spaces, perhaps ? ).

*sigh* did you write this code at random ? So much is broken here, it took me a while to spot the issue.

cmd.Parameters.AddWithValue("@COUNTRY", txtcountry.ToString());
      cmd.Parameters.AddWithValue("@TITLE", txttitle.ToString());
      cmd.Parameters.AddWithValue("@FIRSTNAME", txtfirstname.ToString());
      cmd.Parameters.AddWithValue("@LASTNAME", txtlastname.ToString());
      cmd.Parameters.AddWithValue("@EMPADDRESS", txtempaddress.ToString());
      cmd.Parameters.AddWithValue("@CITY", txtcity.ToString());
      cmd.Parameters.AddWithValue("@ZIP", txtzip.ToString());
      cmd.Parameters.AddWithValue("@EMPSTATE", txtempstate.ToString());
      cmd.Parameters.AddWithValue("@PHONE", txtphone.ToString());
      cmd.Parameters.AddWithValue("@TOPIC", txttopic.ToString());
      cmd.Parameters.AddWithValue("@COMMENTS", txtcomments.ToString());
      cmd.Parameters.AddWithValue("@EMAIL", txtemail.ToString());





ToString将返回控件的名称。 Text属性将返回控件中的Text。学习使用调试器,阅读书籍以学习如何编写代码,而不是仅仅使用自动完成功能并随机抓取内容。



ToString will return the name of the control. The Text property will return the Text in the control. Learn to use a debugger, and read books to learn how to write code, instead of just playing with the autocomplete and grabbing things at random.


这篇关于GridView RowUpdating无法使用ExecuteNonQuery() - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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