Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,i jave a scenario whereby in my query string ,i save it on the databse once i have received it.After that then i go on exttracting those parameters in the string i need.Where i am somehow stuck is updating that query string.On the database it adds two records of one query string .,One for the whole query string and other one for the extracted field items.I s their a way i can prevent this double entry since the whole db has many records mixed up with null values.I need only to update that query string i added first in the database.Thanks

Below is my code

C#
string queryStringData = newString;
               StudentManagement.AddString(queryStringData );





                if (string.IsNullOrEmpty(newString))
                {
                  
                    return;

                }
                else
                {

                    StudentId = int.Parse(context.Request.QueryString["id"].ToString());
                    AdmissionNumber= context.Request.QueryString["AdmNo"];
StudentManagement.AddStudents(StudentId ,AdmissionNumber);



My method to add students
C#
public void AddStudents(StudentId ,AdmissionNumber)
{
Students addStudents=new Students;
StudentId ==StudentId ,
AdmissionNumber==AdmissionNumber
               
 db.Students.InsertOnSubmit(addStudents);

db.SubmitChanges(ConflictMode.FailOnFirstConflict)
}


The add Students method is the one to update the query string saved on the database,Can someone guide on this?Thanks
Posted
Updated 30-Jan-14 22:38pm
v3

1 solution

1. Set constraints to your database to prevent double entries.
2. It looks to me like the SubmitChanges thinks the records is a new one. (You also do Students addStudents=new Students; so it is in fact a new record) --> I assume here that you omitted some lines of code? You should get out the EXISTING value from the db object (or whatever object is bound to this), change the values and then call SubmitChanges.
3. It would help if you would let us know what database you're working on.
4. Are you working with LINQ? in that case the SubmitChanges will generate an SQL which you can see in the output window of Visual Studio. That will immediately tell you if you're inserting or updating.

It's probably better if you update your question with a little more code.

Hope this helps.
 
Share this answer
 
Comments
Member 10398773 31-Jan-14 4:42am    
Infact what anm doing is getting the whole query strung ,adding it to the database(sql),Then i conrtinue on extracting those parameters in the query string,After that i add those values have extracted.I dont know if ican update the first query string with the studentId?anyway thats how am doing it.Have added the line // db.Students.InsertOnSubmit(addStudents);
V. 31-Jan-14 5:30am    
You need to assign a unique key to your student record. When you UPDATE a student, you have a key available and you perform an update, if you do not have a key available it concerns a a new record, hence an insert. Since you're working with LINQ (I assume) this is more or less done for you behind the scenes. The code you posted does not determine for any reader what is happening behind the scenes, but I stick with my first comment. "SubmitChanges" seems to think you're adding a new student.
Member 10398773 31-Jan-14 6:13am    
oooh.The studentiD is the unique key really in my database sir.I have to update it in my code behind?
V. 31-Jan-14 6:18am    
You are not listening to what I'm saying. So for the last time, There is insufficient information in your original post to intelligently respond to this. if my ASSUMPTION is correct LINQ does not seem to recognize the record as an existing one, but as a new one.

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