Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 DataTables
1. DTForQueNewEntry
2. dtFinal

I need to write a code which does the work like following query

SQL
update DTForQueNewEntry SET DTForQueNewEntry.OptionSet = dtFinal.OptionSet

where DTForQueNewEntry.Point_Mapping_Sr= dtFinal.Point_Mapping_Sr AND
    DTForQueNewEntry.Question_Name = dtFinal.Question_Name


where OptionSet, Point_Mapping_Sr, Question_Name are Columns present in both DataTables

Thanks in Advance
Posted

There are two possibility :-

1. Create a SP for the query you have mentioned and call it in your code.

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Databasename"].ConnectionString); {
    using (SqlCommand cmd = new SqlCommand("sp_Name;, con)) {
      cmd.CommandType = CommandType.StoredProcedure;

      con.Open();
      cmd.ExecuteNonQuery();



2. Call the query as it is in your code.

C#
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Databasename"].ConnectionString);
 SqlCommand cmd1 = new SqlCommand("update DTForQueNewEntry SET DTForQueNewEntry.OptionSet = dtFinal.OptionSet 
where DTForQueNewEntry.Point_Mapping_Sr= dtFinal.Point_Mapping_Sr AND
    DTForQueNewEntry.Question_Name = dtFinal.Question_Name");
 con.Open();
cmd1.ExecuteNonQuery();
con.Close();


- NP
 
Share this answer
 
Comments
maverick12131 26-May-13 15:46pm    
Those are not tables in the database. Those are DataTables in C#
Can you please guide me accordingly
I did the following which solved the issue. Thanks

C#
DataRow[] drs = dtWithScore.Select("OptionSet = 'true'");
       DataTable dtFinal = dtWithScore.Clone();
       foreach (DataRow d in drs)
       {
           dtFinal.ImportRow(d);
       }
       for (int i = 0; i < dtFinal.Rows.Count; i++)
       {
           Int64 intPoint_Mapping_Sr = Convert.ToInt64(dtFinal.Rows[i]["Point_Mapping_Sr"]);
           string strQuestionOptions_OptionName = dtFinal.Rows[i]["QuestionOptions_OptionName"].ToString();
           DataRow[] drToupdate = DTForQueNewEntry.Select("Point_Mapping_Sr = " + intPoint_Mapping_Sr + " AND QuestionOptions_OptionName = '" + strQuestionOptions_OptionName + "'");

           drToupdate[0]["OptionSet"] = true;

       }
 
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