0

i want to declare mathematical expression between values from datatable , for example: i have two columns named 'Variable'(string) and 'Contents' (float) , i have inserted in 'Variables' 3 string a , b and c and in 'Contents' 3 floats are 0, 3 and 6 .Now i want to declare 'a=b+c' in code an when i click on a 'button' the variable a will change in datatable to 9. here is my code but it doesn't work!! help me please !

private void button11_Click(object sender, EventArgs e)
    {

        con.Open();            
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from Table_sagemcom ";
        cmd.ExecuteNonQuery();
        DataSet ds1 = new DataSet();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        DataRow dr1 = dt.Select("Variable='a'").FirstOrDefault();
        DataRow dr2 = dt.Select("Variable='b'").FirstOrDefault();
        DataRow dr3 = dt.Select("Variable='c'").FirstOrDefault();

        dr1["Contents"] = Convert.ToDouble(dr2["Contents"]) + Convert.ToDouble(dr3["Contents"]);

        ds1.Tables.Add(dt);

        con.Close();}
Aseljo
  • 17
  • 4
  • 1
    "it doesn't work!! " is not clear. Does your code throws an exceptions? Does it run but some variable's got unexpected value? – Serg Nov 23 '19 at 14:51
  • Possible duplicate of [Update database with changes made to DataTable... confusion](https://stackoverflow.com/questions/15273057/update-database-with-changes-made-to-datatable-confusion) – Eugene Podskal Nov 23 '19 at 14:55
  • @Serg it runs but Nothing is happening th value of 'a' in the table remains unchanged always ='0' – Aseljo Nov 24 '19 at 12:47
  • @Eugene Podskal can you look at my edited answer again ! – Aseljo Nov 24 '19 at 16:29
  • @Aseljo It probably means that something either at `dt.Rows[1]["Variable"]` or `dt.Rows[2]["Variable"]` is not a valid `double`. In any case, without having a full exception stacktrace and knowing the exact contents of those cells, we can only guess. – Eugene Podskal Nov 24 '19 at 19:09
  • @EugenePodskal i I wished that i could find the position of Contents through the names of Variable 'a' , 'b' and 'c' but that solution works with index of rows!! i edited my answer again ! – Aseljo Nov 24 '19 at 22:30
  • There are a lot of info on such an error - https://www.google.com/search?q=System.InvalidOperationException%3A+%27The+dynamic+SQL+generation+of+UpdateCommand+is+not+supported+for+a+SelectCommand+that+does+not+return+key+column+information&rlz=1C1GCEU_ruRU862RU862&oq=System.InvalidOperationException%3A+%27The+dynamic+SQL+generation+of+UpdateCommand+is+not+supported+for+a+SelectCommand+that+does+not+return+key+column+information Have you tried any of the suggestions? – Eugene Podskal Nov 25 '19 at 12:02
  • @Eugene Podskal i tried to solve that error but i fail :( – Aseljo Nov 25 '19 at 12:14
  • That does not tell us what you have tried and how it failed. I recommend you to rewrite the question leaving only the final version of the code and describe what you have tried to solve `The dynamic SQL generation of UpdateCommand is not supported` and how it fails. – Eugene Podskal Nov 25 '19 at 13:40
  • @EugenePodskal i edited my answer just now with my last code and Always datatable still unchanged when i run my code and no errors occured ! – Aseljo Nov 25 '19 at 14:59
  • Probably because you have lost the code from https://stackoverflow.com/a/15293977/3745022 (first comment) . Now there is nothing that actually saves the dataset, so no changes. Well, I recommend to go one step down and try to make the code from https://stackoverflow.com/a/15293977/3745022 actually work for you (I mean the **exact** code without any `Convert.ToDouble(dr2["Contents"]) +` - just plain 1. get data 2. change row 3 save dataset 4. Check that it works). Do not try to solve everything in one go (even when you have some experience and especially if not) - solve everything step by step. – Eugene Podskal Nov 25 '19 at 18:51
  • @EugenePdskal yes i tried that code and it shows me an error that i should declare a primary key System.InvalidOperationException: 'The dynamic SQL generation of UpdateCommand is not supported for a SelectCommand that does not return key column information.' so what should i add in my SQL line to select the primary key?!!! – Aseljo Nov 26 '19 at 09:39

0 Answers0