0
I have in the bank the registration of the building, with the number of rooms qtdSala
, among others. I am trying to insert, in another grid, each room according to the value entered in the qtdSala
, to register on the table rooms, through a for, so I tried so:
private void CriarSalas(int nSalas)
{
for (int i = 0; 1 < nSalas; i++)
{
try
{
SqlConnection con = new SqlConnection(conexaoString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO salas
(nomeSala)
VALUES
(@nomeSala)", con);
cmd.Parameters.AddWithValue("@nomeSala", i);
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
//Atualiza Grid
this.salasTableAdapter.Fill(this.bdDataSet.salas);
SqlConnection con = new SqlConnection(conexaoString);
con.Close();
con.Dispose();
}
}
}
It doesn’t work, I’m not sure how to reference the nsalas
...
He must take the qtdsalas
, 20 for example, and create sala01
, sala02
...until sala20
. Following is the model of the table:
TABELA SALAS
____________________________
| Id | IdPredio | NomeSala |
| 23 | 05 | "Sala01" |
| 24 | 05 | "Sala02" |
| 25 | 05 | "Sala03" |
| 26 | 05 | "Sala04" |
beauty, I liked the tips of good practice, I will follow. But I still have a question: Qdo I say that the amount will be the value of qtdSalas? I do a Where on Insert and then set the amount = qtdSalas.value? or just send amount receive Qtd rooms?
– Tracaja Lima
Edit your question by adding as is the structure of table rooms. Because I didn’t understand your question.
– Thiago Lunardi
Hey, sorry I’m late, I was just doing a little code work. The table rooms will receive the predials, where has the qtySalas, so in the tables rooms will appear the name of the building and sala01, sala02.... only that the rooms should be listed in a single column, what I do not know, in the same idPredio will have as many rooms as they are in the quantity qtdSalas. Could I clarify? I think I have to relate the quantity to the qtdSalas but I don’t know how....
– Tracaja Lima
I edited my answer, see if now answers.
– Thiago Lunardi
I also changed the code comments. :)
– Thiago Lunardi
cmd.Parameters.Addwithvalue("@nameSala", $"Sala0{i}"); I think that’s it, that I didn’t know how to write, I’ll test it and I’ll tell you. Thank you.
– Tracaja Lima
If it works, please evaluate the answer! :)
– Thiago Lunardi
Man, that work gave me to make it work.... but your answer was what helped!! Thank you very much.
– Tracaja Lima