在 ASP.NET 中使用 SqlCommandBuilder

发布于 2024-08-06 02:00:36 字数 1776 浏览 2 评论 0原文

我使用以下代码将输入到 Web 表单的反馈添加到数据库中。

该代码运行良好。但是当我尝试删除这个语句时,

SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);

我收到一个错误,我的意思是它正在执行 else 部分。

谁能告诉我SqlCommandBuilder有什么用?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (txtName.Text.Trim() == "")
    {
        Response.Write("<script>alert ('Name Field cannot be left blank')</script>");
        return;            
    }

    if (txtFeedBack.Text.Trim() == "")
    {
        Response.Write("<script>alert('FeedBack Field cannot be left blank')</script>");
        return;
    }

    objSqlConnection.ConnectionString = connectionStringSetting;
    objSqlConnection.Open();

    try
    {
        SqlDataAdapter objDa = new SqlDataAdapter("select * from FeedBack", objSqlConnection);
        SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);
        DataSet objDs = new DataSet("FeedBack");
        objDa.Fill(objDs, "FeedBack");
        DataRow dr = objDs.Tables["FeedBack"].NewRow();
        dr[1] = txtName.Text;
        dr[2] = txtAddress.Text;
        dr[3] = txtCity.Text;
        dr[4] = txtCountry.Text;
        dr[5] = txtEmail.Text;
        dr[6] = Convert.ToInt32(txtContactNo.Text);
        dr[7] = txtFeedBack.Text;
        objDs.Tables["FeedBack"].Rows.Add(dr);
        objDa.Update(objDs, "FeedBack");
        Response.Write("<script>alert('Your FeedBack has been submitted')</script>");
        objSqlConnection.Close();
    }
    catch (Exception)
    {
        Response.Write("<script> alert('Error on Page. Please try after sometime')</script>");
        objSqlConnection.Close();
    }

有没有办法显示特定的错误消息,例如如果用户输入字符串值而不是整数值,它应该将消息显示为“输入字符串未以正确的格式输入?”

I have used the following code to add the feedback entered to the web form into a database.

The code works fine. But when I try to deleted thi statement

SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);

I am getting an error I mean it is executing the else part.

Can any one tell me what is the use of SqlCommandBuilder?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (txtName.Text.Trim() == "")
    {
        Response.Write("<script>alert ('Name Field cannot be left blank')</script>");
        return;            
    }

    if (txtFeedBack.Text.Trim() == "")
    {
        Response.Write("<script>alert('FeedBack Field cannot be left blank')</script>");
        return;
    }

    objSqlConnection.ConnectionString = connectionStringSetting;
    objSqlConnection.Open();

    try
    {
        SqlDataAdapter objDa = new SqlDataAdapter("select * from FeedBack", objSqlConnection);
        SqlCommandBuilder objcb = new SqlCommandBuilder(objDa);
        DataSet objDs = new DataSet("FeedBack");
        objDa.Fill(objDs, "FeedBack");
        DataRow dr = objDs.Tables["FeedBack"].NewRow();
        dr[1] = txtName.Text;
        dr[2] = txtAddress.Text;
        dr[3] = txtCity.Text;
        dr[4] = txtCountry.Text;
        dr[5] = txtEmail.Text;
        dr[6] = Convert.ToInt32(txtContactNo.Text);
        dr[7] = txtFeedBack.Text;
        objDs.Tables["FeedBack"].Rows.Add(dr);
        objDa.Update(objDs, "FeedBack");
        Response.Write("<script>alert('Your FeedBack has been submitted')</script>");
        objSqlConnection.Close();
    }
    catch (Exception)
    {
        Response.Write("<script> alert('Error on Page. Please try after sometime')</script>");
        objSqlConnection.Close();
    }

And is there any way to display the specific error messages like if an user enters a string value instead of Integer value it should display the message as 'Input String not entered in correct format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-08-13 02:00:36

切勿使用catch(异常)。它隐藏了问题。如果抛出异常,则存在严重错误。你需要了解它。

删除整个 try/catch 块(将代码保留在其中!)。然后再次运行您的代码。如果出现异常,您将在错误页面上看到它。如果没有,请使用事件查看器在 Windows 事件日志中查找来自“ASP.NET”源的警告消息。它应该包含完整的异常。

Never use catch (Exception). It hides the problem. If an exception is being thrown, then there's something serious wrong. You need to learn about it.

Remove the entire try/catch block (keep the code inside it!). Then run your code again. If there's an exception, then you'll see it on the error page. If not, then use the Event Viewer to look in the Windows Event Log for a warning message from the "ASP.NET" source. It should contain the complete exception.

一梦浮鱼 2024-08-13 02:00:36

最终答案是,您正在谈论的线路正在后台执行大量操作。

它将插入、更新和删除命令添加到 DataAdapter。因此,如果没有它,您将崩溃并烧毁,除非您自己将这些命令添加到 DataAdapter 中。

Final answer is that the line you are talking out is doing a bunch of stuff in the background.

It is adding the insert, update and delete commands to the DataAdapter. So without it, you will crash and burn, unless you add those commands yourself to the DataAdapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文