Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

My requirement is, i have textboxes in Gridview Footer, Inserting of records from footer textboxes is working fine for me.

My problem is.. Same record must not be inserted. which is already in database.

When i click add button in gridview footer, a popup message must be displayed. Record already exists.

Please can you show me how to do this, This is my code of Gridview footer in GV ROW COMMAND.

------------------------------------

This is my code.

C#
protected void GV_UserEntry_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox TxtRollNo = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterRollNo);
            TextBox TxtStudentName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterStudentName");
            TextBox TxtStandard = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterStandard");
            TextBox TxtFathersName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterFathersName");
            TextBox TxtMothersName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterMothersName");
            TextBox TxtFamilyName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterFamilyName");
            TextBox TxtLocation = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterLocation");
         
            con.Open();

            SqlCommand cmd = new SqlCommand("INSERT into StudentDetailsTable (RollNo,StudentName,Standard,FathersName,MothersName,FamilyName,Location) VALUES ('" + TxtRollNo .Text + "','" + TxtStudentName .Text + "','" + TxtStandard .Text + "','" + TxtFathersName.Text + "','" + TxtMothersName.Text + "','" + TxtFamilyName.Text + "','" + TxtLocation .Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();


            if (result == 1)
            {
                Bind_StudentDetails();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Student Details Added successfully');", true);
            }

        }

       
    }



Please help thanks.
Posted
Updated 4-Jun-13 23:09pm
v2
Comments
Rockstar_ 5-Jun-13 3:26am    
Use if not exist keywords form sql and try to modify the sql querry, or before inserting the data first check with the columns which you are comparing, then based on the result , insert the data or give error message.

You can do this in two way. One way is add primary key to your table. If try to add same value, it will refuse you.

Second way is before insertion you can get a select query to check whether there is a record same as you are trying to add or not?

In first solution as I remembered you should catch an exception.

To show your message you can use javascript alert.

By the way do not use this

C#
SqlCommand cmd = new SqlCommand("INSERT into StudentDetailsTable (RollNo,StudentName,Standard,FathersName,MothersName,FamilyName,Location) VALUES ('" + TxtRollNo .Text + "','" + TxtStudentName .Text + "','" + TxtStandard .Text + "','" + TxtFathersName.Text + "','" + TxtMothersName.Text + "','" + TxtFamilyName.Text + "','" + TxtLocation .Text + "')", con);


Instead of this, you can use parameters. By using parameter you block all kind of sql injection.

Here is the explanation:

http://msdn.microsoft.com/tr-tr/library/system.data.sqlclient.sqlcommand.parameters.aspx[^]
 
Share this answer
 
Comments
Ubaid ur Rahman IT 5-Jun-13 3:59am    
This is Intranet Application. Please I need it here only.

Please can you help me how to do this.
Osman24 5-Jun-13 4:05am    
Can you be more spesific about what you need. If you are, I can help as much as I can
Ubaid ur Rahman IT 5-Jun-13 4:07am    
Osman brother, I need.... Same record must not be inserted. which is already in database. when add button click in gridview footer...

by the way above is my code.

Please help thanks.
Osman24 5-Jun-13 4:14am    
I understand but here is the thing When you say same record what does it mean. It is all values are same or not?.
Ubaid ur Rahman IT 5-Jun-13 4:20am    
ya, Rollno and StudentName must not be same.

suppose, if Same Rollno and StudentName already exists in database. Show error MSG. (Sorry, this record already exists, Please try another).

If it is new record, it must save in database otherwise already exist error.

Please help

thanks.
It is much easier.

See you can do like this.

as you are going for insert data into Database before that you check that address is already exist by making database call.

Means create another stored procedure that will go and check that same details already exists if yes then call ResgisterStartupScript that will say " Record already exist please enter different record."

or if no then insert data into database and show message record added successfully.
 
Share this answer
 
protected void GV_UserEntry_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox TxtRollNo = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterRollNo);
TextBox TxtStudentName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterStudentName");
TextBox TxtStandard = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterStandard");
TextBox TxtFathersName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterFathersName");
TextBox TxtMothersName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterMothersName");
TextBox TxtFamilyName = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterFamilyName");
TextBox TxtLocation = (TextBox)GV_UserEntry.FooterRow.FindControl("txtFooterLocation");

con.Open();

SqlCommand cmd = new SqlCommand("INSERT into StudentDetailsTable (RollNo,StudentName,Standard,FathersName,MothersName,FamilyName,Location) VALUES ('" + TxtRollNo .Text + "','" + TxtStudentName .Text + "','" + TxtStandard .Text + "','" + TxtFathersName.Text + "','" + TxtMothersName.Text + "','" + TxtFamilyName.Text + "','" + TxtLocation .Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();


if (result == 1)
{
Bind_StudentDetails();
ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Student Details Added successfully');", true);
}

}


}
 
Share this answer
 
v4

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