Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datagrid that loads different columns from SQL database as thus:

ok to be more lucid; each value from the textbox is coming from each column in the datagrid say; Column MATH = 8, Column ENGLIS = 9, Column science = 10; etc. so when each rows is selected each columns shows in textbox1 so my question is; how can i save this figures back to there respective columns in the database; that is say; Column MATH value back to Column MATH and likewise english and science I have tried this so far but I would like to do it without the ',' in the text boxes.


What I have tried:

C#
<pre>SqlConnection Conn = new SqlConnection(shoolmanangmentconn);
        DataTable dt = new DataTable();
        Conn.Open();
        SqlCommand cmd = new SqlCommand("UPDATE tbl_TestingTheApplicationSubject SET MATH=@MATH, ENGLISH=@ENGLISH, SOCIAL=@SOCIAL, SCIENCE=@SCIENCE, RME=@RME, PRETECH=@PRETECH, HOMEECOMICS=@HOMEECOMICS,  HISTORY=@HISTORY, OWOP=@OWOP, ICT=@ICT, FRENCH=@FRENCH, CREATIVEART=@CREATIVEART, LANGUAGE=@LANGUAGE WHERE IDNO=@IDNO", Conn);
        cmd.CommandType = CommandType.Text;
        string[] arryval = txtresults.Text.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
        int i = 0;
        cmd.Parameters.Clear();
        **cmd.Parameters.AddWithValue("@IDNO",txtids.Text)**; // TODO: use correct value of IDNO
        cmd.Parameters.AddWithValue("@ENGLISH", arryval[i++]);
        cmd.Parameters.AddWithValue("@MATH", arryval[i++]);
        cmd.Parameters.AddWithValue("@SOCIAL", arryval[i++]);
        cmd.Parameters.AddWithValue("@SCIENCE", arryval[i++]);
        cmd.Parameters.AddWithValue("@PRETECH", arryval[i++]);
        cmd.Parameters.AddWithValue("@ICT", arryval[i++]);
        cmd.Parameters.AddWithValue("@RME", arryval[i++]);
        cmd.Parameters.AddWithValue("@HOMEECOMICS", arryval[i++]);
        cmd.Parameters.AddWithValue("@HISTORY", arryval[i++]);
        cmd.Parameters.AddWithValue("@OWOP", arryval[i++]);
        cmd.Parameters.AddWithValue("@CREATIVEART", arryval[i++]);
        cmd.Parameters.AddWithValue("@FRENCH", arryval[i++]);
        cmd.Parameters.AddWithValue("@LANGUAGE", arryval[i++]);
        cmd.ExecuteNonQuery();
           }
Posted
Updated 8-Mar-21 0:26am
v2

Why are you doing it like that?
As a UI idea, it's poor - there is no error checking on what the user might have entered incorrectly, no checking that the right number of columns exist, and worse, no checking at all that the entered values are in the expected order.
And that code? You do realise that you enter the same value for every subject multiple times? So at a guess, each time that code is executed, you add 12 rows to your DB?

Instead of doing what is easiest for you as a developer, our job is to make it as easy as possible for the user: and you don't. If I - as a user of your app - had entered the details for a student and miskeyed a 'm' instead of a comma and your app crashed out on me as a result, I wouldn't be happy; and users mistype and miskey all the time!

Seperate your data into separate input boxes, each with a label to prompt for subject, or use a DataGridView to enter the data, and validate it before it gets anywhere near your DB!
Anythign else is just asking for problems that work out very hard to sort out later ...
 
Share this answer
 
Amazing stuff you developed here! What about penomet reviews?
Plz kindly take care!
 
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