0

I want to use the values output by an sql data reader. After modifying I want to update them. I have done the selection part but, I don't know how to use them properly.

 #region Get the value from db
        using (SqlConnection c = new SqlConnection(connect))
        {
            c.Open();
            using (SqlCommand cm = new SqlCommand(com,c))
            {
                cm.Parameters.AddWithValue("@nickname", nick);
                SqlDataReader s = null;
                s = cm.ExecuteReader();
                while (s.Read())
                {
                    string oras = Convert.ToString(s["Oras"]);
                    string judet = Convert.ToString(s["judet"]);
                    string adresa = Convert.ToString(s["adresa"]);
                }
            }
            c.Close();

        }
        #endregion
        textBox1.Text = oras;
        textBox2.Text = judet;
        textBox3.Text = adresa;

3 Answers 3

0

here is common way to do it:

 using (SqlDataReader reader = com.ExecuteReader())
        {
            while (reader.Read())
            {
                My_Event _event = new My_Event();
                if (reader["Id"] != DBNull.Value) { _event.ID = (int)reader["Id"]; }
                if (reader["site"] != DBNull.Value) { _event.Site = reader["site"].ToString(); }
                if (reader["time"] != DBNull.Value) { _event.Time = (DateTime)reader["time"]; }
                   _events.AddFirst(_event);
            }
            reader.Close();
        }

you check if the value is not DBNull.Value and if its not, you cast it to the original type.

3
  • i have done this, but it doesnt get me the value. nick is a global variable made with . in form1 i have global.GlobalVariable= textbox1.text , and here i have nick=Global.GlobalVariable. may this be the problem? Mar 21, 2014 at 10:44
  • put a breakpoint in this function and check if your nick has a value. Moreover, i see that you send parameters somewhere but where? Are you trying to execute a stored procedure, or ur sending sql query?
    – Banana
    Mar 21, 2014 at 10:55
  • i send the parametrs here string com = @"Select (Oras,Judet,Adresa) from informatii where nickname=@nickname"; Mar 21, 2014 at 10:57
0

You need to add and SqlCommand, and Sql Update string and SqlParameters for this:

private static void CreateCommand(string queryString,
string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
           connectionString))
    {
       SqlCommand command = new SqlCommand(queryString, connection);
       command.Connection.Open();
       command.ExecuteNonQuery();
    }
}
2
  • yes , i know but it just doesnt show me the values first. nick is a variable from another form using a global function . form1: global.globalvalue= textbox1.text, and here i have nick=global.globalvalue may this be the problem? Mar 21, 2014 at 10:48
  • @raulbag1996 You should use classes instead of global variables. And isolate the business and data access logic from the view code. Read more about nlayered architecture here, for example: layersample.codeplex.com
    – Oscar
    Mar 21, 2014 at 12:20
0

you've to change your code as follow

#region Get the value from db
        using (SqlConnection c = new SqlConnection(connect))
        {
            c.Open();
            using (SqlCommand cm = new SqlCommand(com,c))
            {
                cm.Parameters.AddWithValue("@nickname", nick);
                SqlDataReader s = null;
                s = cm.ExecuteReader();
                while (s.Read())
                {
                    string oras = Convert.ToString(s["Oras"]);
                    string judet = Convert.ToString(s["judet"]);
                    string adresa = Convert.ToString(s["adresa"]);
                    textBox1.Text = oras;
                    textBox2.Text = judet;
                    textBox3.Text = adresa;
                }
            }
            c.Close();

        }
        #endregion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.