Click here to Skip to main content
12,401,628 members (33,086 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET MySQL
how to insert record in two table that are related.when i insert record then record saved in 1st table but not save in 2nd table

 MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
                    string insquery = "INSERT INTO slayear (YEAR) VALUES(@YEAR)";
                    string insquery1 = "INSERT INTO slamonth (MONTH,contenttype) VALUES(@MONTH,@contenttype)";
 MySqlCommand mycom = new MySqlCommand(insquery, exclecon);
                    mycom.Parameters.AddWithValue("@YEAR", YEAR);
                    MySqlCommand mycom1 = new MySqlCommand(insquery1, exclecon);
                    mycom.Parameters.AddWithValue("@MONTH", MONTH);
                    mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
                   exclecon.Open();
                    mycom.ExecuteNonQuery();
                    mycom1.ExecuteNonQuery();
 con.Close();
}
}
Posted 10-May-13 22:44pm
Updated 10-May-13 23:20pm
Maciej Los221.8K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The object name is wrong:
...
MySqlCommand mycom1 = new MySqlCommand(insquery1, exclecon);
mycom1.Parameters.AddWithValue("@MONTH", MONTH);
mycom1.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
...

This is why it is a really bad practice to use such variable names with a running index at the end. Use a descriptive variable name, don't be lazy: IntelliSense is doing the most of the work. And do't mix statements that belong to each other, and don't use variables you don't really need - you make your code unreadable. I wound organize this code snippet like this:

MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
exclecon.Open();
 
MySqlCommand cmdYear = new MySqlCommand("INSERT INTO slayear (YEAR) VALUES(@YEAR)", exclecon);
cmdYear.Parameters.AddWithValue("@YEAR", YEAR);
cmdYear.ExecuteNonQuery();
 
MySqlCommand cmdMonth = new MySqlCommand("INSERT INTO slamonth (MONTH,contenttype) VALUES(@MONTH,@contenttype)", exclecon);
cmdMonth.Parameters.AddWithValue("@MONTH", MONTH);
cmdMonth.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
cmdMonth.ExecuteNonQuery();
 
exclecon.Close();
  Permalink  
v2
Comments
Maciej Los 11-May-13 6:20am
   
Good advice!
+5
Zoltán Zörgő 11-May-13 15:31pm
   
Thank you
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

object name is wrong in these two lines

mycom.Parameters.AddWithValue("@MONTH", MONTH);
 mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
rectify it to
mycom1.Parameters.AddWithValue("@MONTH", MONTH);
               mycom1.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
  Permalink  
v2
Comments
Zoltán Zörgő 11-May-13 15:32pm
   
Have you read my answer? It's first part is just the same. Don't repeat an answer already given.
Bikash Prakash Dash 12-May-13 2:44am
   
IS DER ANY PROBLEM, I'M GIVING SOLUTION IN SHORT .
Zoltán Zörgő 12-May-13 6:35am
   
Don't shout! No, you are not giving a new solution, You have repeated min - without understanding it. And you yell at me, that's rude.
Bikash Prakash Dash 12-May-13 7:12am
   
I'M NOT SHOUTING , I'M JUST TYPING , HOW CAN I SHOUT IN CODEPROJECT , ANY WAYS SOMEHOW IF I'VE REPEATED, I DON'T THINK ITS AN OFFENCE. SO WHY ARE YOU GETTING ANGRY, COOL MAN , ITS A TECHNICAL WEBSITE , WHERE WE ARE SHARING OUR TECHNICAL SKILLS AND HELP EACH OTHER.
Zoltán Zörgő 12-May-13 15:25pm
   
Angry, me? No way. Yes, you are shouting - since you use all capital letters. Read this one: http://www.codeproject.com/Articles/64628/Code-Project-Quick-Answers-FAQ#rules paragraph 4.8. But "all caps" is shouting since the internet exists, so please behave your virtual self.
Ah, and by the way, you should "reply" to a comment, if you want the other party to be notified.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100