Click here to Skip to main content
14,836,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
 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
Updated 10-May-13 23:20pm
v2

The object name is wrong:
C#
...
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:

C#
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();
   
v2
Comments
Maciej Los 11-May-13 6:20am
   
Good advice!
+5
Zoltán Zörgő 11-May-13 15:31pm
   
Thank you
object name is wrong in these two lines

C#
mycom.Parameters.AddWithValue("@MONTH", MONTH);
 mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;

rectify it to
C#
mycom1.Parameters.AddWithValue("@MONTH", MONTH);
               mycom1.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;
   
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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900