-4

I have a project in C # to get about 50 text boxes and then stored in the database. This is done using the function. The only problem is I'm making function. I thank you. What needs to be done.

x8 = int.Parse( txtt10x100t.Text);
                if (x8 > 0)
                {
                    SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=database;Integrated Security=True");
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cn;
                    cmd.CommandText = "INSERT INTO tbltable " + "(id, name, value , date, iid) " + "VALUES(@id, @name, @value, @date, @iid )";
                    cmd.Parameters.AddWithValue("@id", v2);
                    cmd.Parameters.AddWithValue("@name", "10*100");
                    cmd.Parameters.AddWithValue("@value", x8);
                    cmd.Parameters.AddWithValue("@date", tarikh);
                    cmd.Parameters.AddWithValue("@iid", xxiid);
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cn.Close();
 x9 = int.Parse(txtt10x150t.Text);
                    if (x9 > 0)
                    {
                        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=dbdatabase;Integrated Security=True");
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = cn;
                        cmd.CommandText = "INSERT INTO tbltable " + "(id, name, value, date, iid) " + "VALUES(@id, @name, @value, @date, @iid )";
                        cmd.Parameters.AddWithValue("@id", v2);
                        cmd.Parameters.AddWithValue("@name", "10*150");
                        cmd.Parameters.AddWithValue("@value", x9);
                        cmd.Parameters.AddWithValue("@date", tarikh);
                        cmd.Parameters.AddWithValue("@iid", xxiid);
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
x10 = int.Parse(txtt15x100t.Text);
                    if (x10 > 0)
                    {
                        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=dbdatabase;Integrated Security=True");
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = cn;
                        cmd.CommandText = "INSERT INTO tbltable " + "(id, name, value, date, iid) " + "VALUES(@id, @name, @value, @date, @iid )";
                        cmd.Parameters.AddWithValue("@id", v2);
                        cmd.Parameters.AddWithValue("@name", "15*100");
                        cmd.Parameters.AddWithValue("@value", x10);
                        cmd.Parameters.AddWithValue("@date", tarikh);
                        cmd.Parameters.AddWithValue("@iid", xxiid);
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();

// , ete ....
  • 3
    What's your problem now? – Sirwan Afifi Nov 27 '15 at 05:44
  • All information text boxes should be stored in the same way. It must be written for each text box. Need to function is to save all text boxes. How function to be written? – webnevesht Nov 27 '15 at 05:50
  • http://stackoverflow.com/a/5495466/1646540 – Sirwan Afifi Nov 27 '15 at 05:56
  • While online translation tools such as Microsoft Translator work reasonably well, if you cannot express yourself in English, you should use those tools yourself to translate the text _before_ posting. Foreign language comments will have a very limited audience and don't fit the English standard in use on Stack Overflow. – Peter Duniho Nov 27 '15 at 05:59

1 Answers1

1

Your are not asking how to convert the code into a function, are you?

if so, learn coding. In the meantime here is what the function would look like:

int MyFunc(string txtboxtext)
{
    var x8 = int.Parse(txtboxtext);
                    if (x8 > 0)
                    {
                        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=database;Integrated Security=True");
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = cn;
                        cmd.CommandText = "INSERT INTO tbltable " + "(id, name, value , date, iid) " + "VALUES(@id, @name, @value, @date, @iid )";
                        cmd.Parameters.AddWithValue("@id", v2);
                        cmd.Parameters.AddWithValue("@name", "10*100");
                        cmd.Parameters.AddWithValue("@value", x8);
                        cmd.Parameters.AddWithValue("@date", tarikh);
                        cmd.Parameters.AddWithValue("@iid", xxiid);
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
                   }
    }

But you should not use that code at all, because you should not open a connection for each database access. And the function would need to have the other variables like tarikh and so passed as parameter. I don't know their types, so I couldn't include them into the parameter list. And even if you did, it would be better to create a class to group these variables into a meaningful unit.

Did I miss something?

Sascha
  • 1,190
  • 1
  • 16
  • 31
  • Yes you did :P, the `@name` is also a value ;-) – WiiMaxx Nov 27 '15 at 07:57
  • Oh, right. I should add to my above list, that "10*100" is a constant and could have been inserted into the commandtext. But then we would have to discuss the meaning of storing constants in a database. – Sascha Nov 27 '15 at 08:18