在同一个按钮上插入和更新。 [英] Insert and Update on the same button.

查看:72
本文介绍了在同一个按钮上插入和更新。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web表单,用户可以在其中输入数据并将其保存到数据库中。我还可以将用户返回并更新他们提交到数据库中的数据。当用户返回到Web表单并编辑其数据时,如何才能更新数据,而不是在同一按钮上记录已存在的另一条记录?这是我的保存按钮代码:



  protected   void  ButtonSave_Click( object  sender,EventArgs e)
{
SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ HotConnectionString]的ConnectionString);
con7.Open();

SqlCommand cmd = new SqlCommand( 插入表445(User_ID,FT_UNDERGR,DATE,FT_GRAD,FTE_UNDERG,FTE_GRAD,NON_CREDIT,TOTAL_FTE,FCFTUHC,FCFTPBHC,FCPTUHC,FCPTPBHC,NCHC,UnderG12,Postb9,Total123b4b,FTEYR,THCAS,FTE40,HC50)值(@ User_ID,@ FT_UNDERGR,@ DATE,@ FT_GRAD,@ FTE_UNDERG,@ FTE_GRAD,@ NON_CREDIT,@ TOTAL_FTE,@ FCFTUHC,@ FCFTPBHC,@ FCPTUHC,@ FCPTPBHC,@ NCHC,@ UnderG12,@ Postb9,@ Total123b4b,@ FIGEYR, @THCAS,@ FTE40,@ HC50);,con7);

cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue( @ INST_ID,TextBoxINST_ID.Text );
cmd.Parameters.AddWithValue( @ FT_UNDERGR,TextBoxFTUG.Text.Replace(< span class =code-string> ));
cmd.Parameters.AddWithValue( @ FT_GRAD,TextBoxFTG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FTE_UNDERG,TextBoxTHUGDR.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FTE_GRAD,TextBoxTHGDR.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ NON_CREDIT,TextBoxNCCDR.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ TOTAL_FTE,TextBoxTCNC.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FCFTUHC,TextBoxTNFUG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FCFTPBHC,TextBoxTNFG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FCPTUHC,TextBoxTNCPUG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FCPTPBHC,TextBoxTNCPG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ NCHC,TextBoxTNNCC.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FTEYR,lblYEAR1.Text);
cmd.Parameters.AddWithValue( @ DATE,TextBoxDATE.Text);
cmd.Parameters.AddWithValue( @ UnderG12,TextBoxTHUG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ Postb9,TextBoxTHG.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ Total123b4b,TextBoxT1234.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ THCAS,TextBoxTHCAS.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ FTE40,TextBoxFTE40.Text.Replace(< span class =code-string>
));
cmd.Parameters.AddWithValue( @ HC50,TextBoxHC50.Text.Replace(< span class =code-string>
));


cmd.ExecuteNonQuery();

if (Page.IsPostBack)
{
ScriptManager.RegisterStartupScript( this typeof (页面), myscript alert('您已成功保存了注册资料的电子部分。您将现在直接到毕业率信息页'); location.href ='Gradrate.aspx'; true );
}
}
}





有没有办法将插入和更新放在相同的按钮?



这是我用来检查用户是否存在的代码。我是否可以将相同的代码用于记录以查看它是否存在并更新(如果存在)并插入(如果不存在)?



  if (IsPostBack)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager .ConnectionStrings [ HotConnectionString]。ConnectionString);
con.Open();
string cmdStr = 选择计数(* )来自Table99,其中EmailAddress =' + TextBoxEA.Text + ';
SqlCommand userExist = new SqlCommand(cmdStr,con);
SqlCommand cmd = new SqlCommand( select来自Table99的USer_ID,EmailAddress,con);
int temp = Convert.ToInt32(userExist.ExecuteScalar()。ToString());
if (temp == 0
{
ScriptManager。 RegisterStartupScript( this this .GetType(), script alert('用户名不存在你必须先填写注册'); true );
TextBoxEA.Text = string .Empty;
}
else if (temp == 1
{
ScriptManager.RegisterStartupScript( this this .GetType(), script alert('无效的用户名/密码'); true );
}

解决方案

我不清楚

引用:

我也有用户可以回来并更新他们提交到数据库中的数据





假设一个保存按钮和一个数据输入表格



在保存按钮代码中,



你可以有一个选择查询来验证数据是否存在于数据库中。

如果存在则

更新

其他

插入

结束如果





如需样品,请参阅下面的内容。



http://www.dotnetspider.com/论坛/ 223349-asp-net-with-C-insert-update-save-button.aspx [ ^ ]


I have a web form to where a user can enter data and save it to the database. I also have it to where the user can come back and update the data they submitted into the database. When the user comes back to the web form and edits their data, how can I get the data to update and not insert another record it the record already exists on the same button? Here is my save button code:

protected void ButtonSave_Click(object sender, EventArgs e)
    {
        SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con7.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table445 (User_ID, FT_UNDERGR, DATE, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, UnderG12, Postb9, Total123b4b, FTEYR, THCAS, FTE40, HC50) values (@User_ID, @FT_UNDERGR, @DATE, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTPBHC, @NCHC, @UnderG12, @Postb9, @Total123b4b, @FTEYR, @THCAS, @FTE40, @HC50);", con7);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@FT_UNDERGR", TextBoxFTUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FT_GRAD", TextBoxFTG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTE_UNDERG", TextBoxTHUGDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@NON_CREDIT", TextBoxNCCDR.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@TOTAL_FTE", TextBoxTCNC.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCFTUHC", TextBoxTNFUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCPTUHC", TextBoxTNCPUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FCPTPBHC", TextBoxTNCPG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@NCHC", TextBoxTNNCC.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTEYR", lblYEAR1.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@UnderG12", TextBoxTHUG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@Postb9", TextBoxTHG.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@Total123b4b", TextBoxT1234.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@THCAS", TextBoxTHCAS.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@FTE40", TextBoxFTE40.Text.Replace(",", ""));
        cmd.Parameters.AddWithValue("@HC50", TextBoxHC50.Text.Replace(",", ""));
        

        cmd.ExecuteNonQuery();

        if (Page.IsPostBack)
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "alert('You have successfully saved the electronic portion of the Enrollment Profile. You will now be direct to the Graduation Rate Information Page');location.href='Gradrate.aspx';", true);
        }
    }
}



Is there a way to put the Insert and Update on the same button?

Here is a code I used for checking to see if a user exists. Can I use this same code for the record to see if it exists and update if it does and insert if not?

if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select count(*) from Table99 where EmailAddress='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select USer_ID, EmailAddress from Table99", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('User Name Does Not Exist You Must Fill Out Registration First');", true);
                TextBoxEA.Text = string.Empty;
            }
            else if (temp == 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invalid UserName / Password');", true);
            }

解决方案

I am not clear with

Quote:

I also have it to where the user can come back and update the data they submitted into the database



Assuming One save button and a data entry form

In save button code ,

You can have a select query to validate whether the data exist in database .
If exist then
Update
else
Insert
End if


For a sample please refer below.

http://www.dotnetspider.com/forum/223349-asp-net-with-C-insert-update-save-button.aspx[^]


这篇关于在同一个按钮上插入和更新。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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