0

As I'm not programming long time I would like to ask you if there is way to call result of this SqlCommand which is in class called klientClass

I was thinking that it could look something like this:

private static void ReadFirma()
    {
        string queryString =
            "SELECT rocnik from FIRMA;";
        using (SqlConnection connection = new SqlConnection(myConnection.DataSource.ConnectionString
                   ))
        {
            SqlCommand command = new SqlCommand(
                queryString, connection);
            connection.Open();

            int result= Convert.ToInt32(command.ExecuteScalar());
            try
            {

            }
            finally
            {

                reader.Close();
            }
        }
    }

Because I need to insert this result into my report parameter here:

 this.klientTableAdapter.Fill(this.prehled_zajezdu.HereReturnResult);

        this.reportViewer1.RefreshReport();

I'm sorry for quiet low-quality question, hope not to receive down-votes.

8
  • You can use dataset or datatable fill by using DataAdapter. Commented Aug 21, 2013 at 10:58
  • i don't understand the question. can you be more clear about it? yes you can run this function and you can call it, what doesn't work for you? Commented Aug 21, 2013 at 10:58
  • @NoIdeaForName Well I'm not sure how can I call exactly the result of the SqlCommand, would you help me pleasE? Thanks in advance. Commented Aug 21, 2013 at 10:59
  • Where do you need to use the result? Your question is lacking in describing what you want to achieve. A TableAdapter takes either a DataTable or a DataSet as argument to the Fill method. In order to get a scalar, you don't need a DataReader. The ReadFirma method should probably return something. Commented Aug 21, 2013 at 10:59
  • see this example : stackoverflow.com/questions/11993211/… Commented Aug 21, 2013 at 11:00

2 Answers 2

1

This is how you can retrieve and use the value from the database in your Fill method (provided that the Fill method takes an argument of the type int and that the myConnection field is available from the static method)

private static int ReadFirma()
{
    string queryString = "SELECT rocnik from FIRMA";
    using (var connection = 
        new SqlConnection(myConnection.DataSource.ConnectionString))
    using(var command = new SqlCommand(queryString, connection))
    {
        connection.Open();
        return Convert.ToInt32(command.ExecuteScalar());
    }
}


void SomeMethod()
{
    this.klientTableAdapter.Fill(ReadFirma());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, you understood exactly what I meant even that I described that very badly. May I ask how do I call this ReadFirma() to another class where is klint Tableadapter.Fill pleasE? Thanks in advance.
I got that I set only private static int to public static int. Thanks so much
0

You can use DataTable object for your Goal.

private static DataTable ReadFirma()
{
    string queryString = "SELECT rocnik from FIRMA";
    using (var connection = 
        new SqlConnection(myConnection.DataSource.ConnectionString))
    using(var command = new SqlCommand(queryString, connection))
    {
        connection.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = command;
        da.Fill(dt);
        return dt;
    }
}


void SomeMethod()
{
    this.klientTableAdapter.Fill(ReadFirma());
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.