Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to update perticular row of grid ....
means on save button i was written insert query but when i want to update then update query is also written in save button but it not update record...it generate new record....
so how can i check condition of insert or update???
plz help me.....
thanks in advance.


my code is:-
C#
public void SysATPLProcSave()
   {
       string firstname = TxtATFistName.Text.Trim();
       string middlename = TxtATMiddleName.Text.Trim();
       string lastname = TxtATLastName.Text.Trim();
       string mail = TxtATEmail.Text.Trim();

       string homeaddress = TxtATHomeAddress.Text;
       string homecity = TxtATHomeCity.Text.Trim();
       string homestate = TxtATHomeState.Text.Trim();
       string homezipcode = TxtATHomeZipcode.Text.Trim();
       string homecountry = TxtATHomeCountry.Text.Trim();
       string homephone = TxtATHomePhone.Text.Trim();
       string homefax = TxtATHomeFax.Text.Trim();
       string homemobile = TxtATHomeMobile.Text.Trim();

       string businesscompany = TxtATBusinessCompany.Text.Trim();
       string businessaddress = TxtATBusinessAddress.Text;
       string businesscity = TxtATBusinessCity.Text.Trim();
       string businesstate = TxtATBusinessState.Text.Trim();
       string businesszip = TxtATBusinessZip.Text.Trim();
       string businesscountry = TxtATBusinessCountry.Text.Trim();
       string businessjob = TxtATBusinessJob.Text.Trim();
       string businessdepartment = TxtATBusinessDepartment.Text.Trim();
       string businessoffice = TxtATBusinessOffice.Text.Trim();
       string businessphone = TxtATBusinessPhone.Text.Trim();
       string businessfax = TxtATBusinessFax.Text.Trim();



       string contactlist = "INSERT INTO QAFranchise.HDR_CONTACT_LIST(PROFILE_ID,CONTACT_LIST_ID,CONTACT_FIRST_NAME,CONTACT_MIDDLE_NAME,CONTACT_LAST_NAME,CONTACT_EMAIL,CONTACT_HOME_STREET_ADDRESS,CONTACT_HOME_CITY,CONTACT_HOME_STATE_PROVINCE,CONTACT_HOME_ZIP_CODE,CONTACT_COUNTRY_REGION,CONTACT_HOME_PHONE,CONTACT_HOME_FAX,CONTACT_HOME_MOBILE,CONTACT_COMPANY_NAME,CONTACT_BUSINESS_STREET_ADDRESS,CONTACT_BUSINESS_CITY,CONTACT_BUSINESS_STATE_PROVINCE,CONTACT_BUSINESS_ZIP_CODE,CONTACT_BUSINESS_COUNTRY_REGION,CONTACT_BUSINESS_JOB_TITLE,CONTACT_BUSINESS_DEPARTMENT,CONTACT_BUSINESS_OFFICE,CONTACT_BUSINESS_PHONE,CONTACT_BUSINESS_FAX,ADD_USER_ID,ADD_USER_DATE) " +
           "Values(59,QAFranchise.GET_CONTACT_LIST_ID(59),'" + firstname + "','" + middlename + "','" + lastname + "','" + mail + "','" + homeaddress + "','" + homecity + "','" + homestate + "','" + homezipcode + "','" + homecountry + "','" + homephone + "','" + homefax + "','" + homemobile + "','" + businesscompany + "','" + businessaddress + "','" + businesscity + "','" + businesstate + "','" + businesszip + "','" + businesscountry + "','" + businessjob + "','" + businessdepartment + "','" + businessoffice + "','" + businessphone + "','" + businessfax + "',1,getdate())";
       SqlCommand cmd = new SqlCommand(contactlist, cn);
       cmd.ExecuteNonQuery();

   }




cant take update button so want to make two operation on single button so plz help me.....

string contactlist = "update QAFranchise.HDR_CONTACT_LIST where CONTACT_FIRST_NAME='" + TxtATFistName.Text + "',CONTACT_MIDDLE_NAME='" + TxtATMiddleName.Text + "',CONTACT_LAST_NAME='" + TxtATLastName.Text + "',CONTACT_EMAIL='" + TxtATEmail.Text + "',CONTACT_HOME_STREET_ADDRESS='" + TxtATHomeAddress.Text + "',CONTACT_HOME_MOBILE='" + TxtATHomeMobile.Text + "'";
SqlCommand cmd1 = new SqlCommand(contactlist, cn);
cmd1.ExecuteNonQuery();



edit button is also here but first i select edit button then select row of grid then that row data comes into textboxes and then i save(update ) data so i was writeen update query into save button but there also insert query so double data insert into table so how can i check condition????


we cannot take hiddenfeild and set value=0 bcoz i my table multiple value so i can select any one so we cant use...
so plz give me another solution.....
plz plz help me.
Posted
Updated 8-Nov-12 0:40am
v6
Comments
helloworld2020 8-Nov-12 4:28am    
Provide more information!
AshishChaudha 8-Nov-12 4:31am    
Share your code for better help..
helloworld2020 8-Nov-12 4:47am    
Where is your Update query?!!
helloworld2020 8-Nov-12 4:53am    
And why you're insisting using just one button for Save and Update? It will be better if you put two buttons. Otherwise it would be confusing

try something like this:

C#
  using (SqlCommand cmd =
 new SqlCommand("SELECT Profile_ID FROM YourTable", cn))

 SqlDataReader reader = cmd.ExecuteReader();

 if (reader.HasRows)
 {
     while (reader.Read())
     {
         String textReaded = reader["Profile_ID"].ToString();

     }
 }

if (txtReaded !=null)
 {
     // your update query
 }
 else
 {
   // your insert query
 }

You need to read something from database, than compare if it is null. If compared cell is null you need to insert data, otherwise update it.
 
Share this answer
 
v2
take hiddenfield and when You edit data of gridview You set hiddenfield value to command argument of gridview so when You update, hiddenfield will have value and on page load in not ispostback clear value of hiddenfield and after submit also clear value of hiddenfield
Your condition would be on submit button click
if (hdSrNo.value)>0
{
//update
}
else
{
//insert
}
hope this will help You..
 
Share this answer
 

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