如何给出记录已存在于数据库中的消息

如何给出记录已存在于数据库中的消息

本文介绍了如何给出记录已存在于数据库中的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的插入选项代码



this my code for insert option

protected void Insert(object sender, EventArgs e)
        {
            string ConnectionString = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
            SqlConnection sqlconnection = null;
          
        int retVal=0;
                TextBox cust_id = (TextBox)GridView1.FooterRow.FindControl("txtCustId");

                TextBox name = (TextBox)GridView1.FooterRow.FindControl("txtName");

                TextBox state = (TextBox)GridView1.FooterRow.FindControl("txtState");

                TextBox s_amount = (TextBox)GridView1.FooterRow.FindControl("txtAmnt");

                TextBox sales_id = (TextBox)GridView1.FooterRow.FindControl("txtSid");

                try
                {

                    using (sqlconnection = new SqlConnection(ConnectionString))
                    {
                        sqlconnection.Open();
                        SqlCommand cmd = new SqlCommand("GetSubmitDetails", sqlconnection);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@cust_id", SqlDbType.Int).Value = cust_id.Text;
                        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
                        cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = state.Text;
                        cmd.Parameters.Add("@s_amount", SqlDbType.VarChar).Value = s_amount.Text;
                        cmd.Parameters.Add("@sales_id", SqlDbType.Int).Value = sales_id.Text;

                        cmd.ExecuteNonQuery();
                      
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine("Error reading from {0}", ex);
                }
                finally
                {
                    sqlconnection.Close();
                }
            
            if (retVal == 2)
            {
                  Label1.Text = "Records exist..!!!!!";
            }
            else
            {
            GridView1.EditIndex = -1;
            Label1.Visible = true;
            Label1.Text = "Records are Submitted Successfully";
            GridView1.DataBind();
            GetData();
            }
       
    }

    

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GetData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string cust_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sales_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
           
            string ConnectionString = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
            SqlConnection sqlconnection = null;
            try
            {

                using (sqlconnection = new SqlConnection(ConnectionString))
                {
                
                    sqlconnection.Open();
                  
                    SqlCommand cmd = new SqlCommand("GetDeleteItems", sqlconnection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@cust_id", SqlDbType.Int).Value = cust_id as String;
                    cmd.Parameters.Add("@sales_id", SqlDbType.Int).Value = sales_id as String;
                  

                  

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    cmd.ExecuteNonQuery();
                   
                    Label1.Text = "Records are deleted successfully";
                    GetData();
                   
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading from {0}", ex);
            }
            finally
            {
                sqlconnection.Close();
            }
         
        }

推荐答案

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('my message');", true);


protected void Insert(object sender, EventArgs e)
        {
            string ConnectionString = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
            SqlConnection sqlconnection = null;
          
        int retVal=0;
                TextBox cust_id = (TextBox)GridView1.FooterRow.FindControl("txtCustId");
 
                TextBox name = (TextBox)GridView1.FooterRow.FindControl("txtName");
 
                TextBox state = (TextBox)GridView1.FooterRow.FindControl("txtState");
 
                TextBox s_amount = (TextBox)GridView1.FooterRow.FindControl("txtAmnt");
 
                TextBox sales_id = (TextBox)GridView1.FooterRow.FindControl("txtSid");
 
                try
                {
 
                    using (sqlconnection = new SqlConnection(ConnectionString))
                    {
                        sqlconnection.Open();
                        SqlCommand cmd = new SqlCommand("GetSubmitDetails", sqlconnection);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@cust_id", SqlDbType.Int).Value = cust_id.Text;
                        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text;
                        cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = state.Text;
                        cmd.Parameters.Add("@s_amount", SqlDbType.VarChar).Value = s_amount.Text;
                        cmd.Parameters.Add("@sales_id", SqlDbType.Int).Value = sales_id.Text;
 
                        cmd.ExecuteNonQuery();
                      
                    }
                }
 
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading from {0}", ex);
                }
                finally
                {
                    sqlconnection.Close();
                }
            
            if (retVal == 2)
            {
                  ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('Record Exists !!!');", true);
            }
            else
            {
            GridView1.EditIndex = -1;
           ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('Record Saved Sucessfully');", true);
            GridView1.DataBind();
            GetData();
            }
       
    }
 
    
 
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GetData();
        }
 
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
 
            string cust_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sales_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
           
            string ConnectionString = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
            SqlConnection sqlconnection = null;
            try
            {
 
                using (sqlconnection = new SqlConnection(ConnectionString))
                {
                
                    sqlconnection.Open();
                  
                    SqlCommand cmd = new SqlCommand("GetDeleteItems", sqlconnection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@cust_id", SqlDbType.Int).Value = cust_id as String;
                    cmd.Parameters.Add("@sales_id", SqlDbType.Int).Value = sales_id as String;
                  
 
                  
 
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    cmd.ExecuteNonQuery();
                   
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('Record Deleted Sucessfully');", true);
                    GetData();
                   
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading from {0}", ex);
            }
            finally
            {
                sqlconnection.Close();
            }
         
        }


这篇关于如何给出记录已存在于数据库中的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 16:52