Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected void Insert(object sender, EventArgs e)
{
string ConnectionString = WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
SqlConnection sqlconnection = null;


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();
SqlDataReader sqlReader =cmd.ExecuteReader();

if (sqlReader.HasRows)
{
while (sqlReader.Read())
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Record exist');", true);
}
sqlReader.Close();
}

}
}

catch (Exception ex)
{
Console.WriteLine("Error reading from {0}", ex);
}
finally
{
sqlconnection.Close();
}



GridView1.EditIndex = -1;


ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Record Saved Sucessfully');", true);
GridView1.DataBind();
GetData();


}
Posted

Hi,,

Add one label in your web page like <asp:label id="lblsuccess" runat="server" xmlns:asp="#unknown">

and in your c# code if condition is saticified then just write
labelId.Text = "<b style="color:green">Details submitted Succesfully</b>";
 
Share this answer
 
v2
Comments
Member 11704156 20-May-15 2:57am    
my code displays records submitted successfully... how about displaying record already exist ?
int record= Convert.ToInt32(cmd3.ExecuteScalar());
string name = txtfname.Text;
if (record > 0)
{
Response.Write(
"<h4 style='color:red'>mail already exists</h4>"
);
}
 
Share this answer
 
Comments
Shivaram_i 20-May-15 3:07am    
Hope this is useful for you
Member 11704156 20-May-15 3:17am    
in asp.net can v write response.write
Shivaram_i 20-May-15 3:20am    
no u cant write that in asp.net..it is c# code...After checking the condition like
"SqlCommand cmd = new SqlCommand("select count (*) from cntus where id("mention the field name which one u dont want to enter duplicate values")='" + txtboxId.Text + "'", con);"
int record= Convert.ToInt32(cmd3.ExecuteScalar());
string name = txtfname.Text;
if (record > 0)
{
Response.Write(
"

mail already exists

");
}
Member 11704156 20-May-15 3:24am    
for stored procedure hw v can write
Member 11704156 20-May-15 3:24am    
this s my stored procedure hw can v take dis message to UI part



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

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