Hi Guys,

I am using sql in and am stuck. after "selecting * from table", how do i store whatever I have obtained from my database in a variable, for display?

Thanks.

Recommended Answers

All 4 Replies

Post the code where you are executing the select * from table . There are a number of ways to go about this.

OK, am having something like this:

.............................
.............................
Dim SQLConn As New SqlConnection
        Dim SQLCmd As New SqlCommand
        SQLConn.ConnectionString = ConnStr
        SQLConn.Open()
        SQLStr = "use Institution" 'This is my database
        SQLCmd.CommandText = SQLStr
        SQLCmd.Connection = SQLConn
        SQLCmd.ExecuteNonQuery()
        SQLStr = "Select * from Stud_Details where 1"
        SQLCmd.CommandText = SQLStr
        SQLCmd.Connection = SQLConn
        SQLCmd.ExecuteNonQuery() 'The problem begins here! Is it OK?

No .. that isn't ok. You need to load the results in to a DataTable using a SqlDataReader created by calling SqlCommand.ExecuteReader() .

Example:

private void button3333_Click(object sender, EventArgs e)
    {
      const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      const string query = "Select * From Invoice (NOLOCK)";

      DataTable dt;

      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          using (SqlDataReader dr = cmd.ExecuteReader())
          {
            dt = new DataTable();
            dt.Load(dr); //This loads a table
          }
        }
        conn.Close();
      }

      MessageBox.Show(string.Format("You have {0:F0} rows", dt.Rows.Count));
    }

thanks alot, it just worked.
thanks for your help.

regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.