如何更新网格数据 [英] how to update grid data

查看:96
本文介绍了如何更新网格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新网格的垂直行....
意思是在保存按钮上我被写为插入查询,但是当我要更新时,更新查询也写在保存按钮上,但是它不更新记录...它生成新记录....
所以我该如何检查插入或更新的条件??
请帮助我.....
在此先感谢.


我的代码是:-

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:-

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();

   }





无法获取更新按钮,所以想对单个按钮进行两次操作,所以请帮助我.....

字符串contactlist =更新QAFranchise.HDR_CONTACT_LIST,其中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 =新的SqlCommand(contactlist,cn);
cmd1.ExecuteNonQuery();



编辑按钮也在这里,但首先我选择编辑按钮,然后选择网格的行,然后将该行数据放入文本框,然后我保存(更新)数据,因此我将更新查询写入保存按钮,但也插入了查询,因此将双精度数据插入到表,以便如何检查状况???


我们不能采用hiddenfeild并将value = 0 bcoz设置为我的表的多个值,因此我可以选择任何一个,所以我们不能使用...
所以请给我另一个解决方案.....
请帮助我.





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.

推荐答案

尝试如下操作:

try something like this:

  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
 }


您需要从数据库中读取某些内容,然后比较是否为空.如果比较的单元格为null,则需要插入数据,否则进行更新.


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.


获取hiddenfield,并且当您编辑gridview的数据时,将hiddenfield值设置为gridview的命令参数,因此在更新时,hiddenfield将具有值,并在页面加载时处于非ispostback的hiddenfield清除值,并且提交后也具有hiddenfield的清除值
您的条件将是在提交"按钮上单击
如果(hdSrNo.value)> 0
{
//更新
}
其他
{
//插入
}
希望对您有帮助.
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..


这篇关于如何更新网格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆