Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this my code for insert option

C#
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();
            }
         
        }
Posted
Updated 19-May-15 18:40pm
v2
Comments
PIEBALDconsult 20-May-15 0:41am    
You have posted too much code and not enough question.
Please use Improve question to remove the irrelevent code and add detail and context.

Instead of using label to display Record already Exists, use the below code to show meesage in pop up

C#
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('my message');", true);
 
Share this answer
 
Comments
Member 11704156 20-May-15 0:41am    
where can v use this
Member 11704156 20-May-15 0:56am    
i want to display record submitted successfully and record exist ...
C#
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();
            }
         
        }
 
Share this answer
 
Comments
Member 11704156 20-May-15 1:36am    
its showing record exist for both condition if i add new record it shows exist and add same it shows exist ...
i have written a stored procedure
Member 11704156 20-May-15 1:36am    
ALTER procedure [dbo].[GetSubmitDetails]
(
@cust_id int ,
@name varchar (50),
@state Varchar (50),
@s_amount varchar (50),
@sales_id int


)

AS
declare @count int
set @count = (SELECT count(*) FROM customer inner join
sales
on customer.cust_id=sales.cust_id
WHERE customer.cust_id = @cust_id
and sales.sales_id=@sales_id)
if(@count>0)
BEGIN

SELECT 'This record already exists!'
END
ELSE

BEGIN


SELECT 'Record Added'
Insert into test.dbo.[customer](cust_id,name,[state]) values (@cust_id,@name,@state)

Insert into test.dbo.[sales](cust_id,sales_id,s_amount) values (@cust_id,@sales_id,@s_amount)
END
Member 11704156 20-May-15 1:37am    
how to take dis message from stored procedure and display in UI

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900