locked
SQLConnection RRS feed

  • Question

  • Now I want to connect my C#/XAML code to my sqlserver database and get the max id in the stock table.

    There's another problem that I hadn't made any Datasets or Connectionstrings through visual studio and when I try to .. I don't find my server while creating new connection for the new dataset, actually it's a null list.

    Here's my code

    int maxstockid; //Identify the maximum ID of current Stocks
                //Command to Get the maximum stock ID.
                string queryString = "select max (Inventory_ID) from dbo.Stocks;";
                using (SqlConnection connection = new SqlConnection("Data Source = MRBRILLIANTPC; Initial Catalog = AGDB; Integrated Security = True"))
                {
                    SqlCommand command = new SqlCommand( queryString, connection);
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    try
                    {
                        while (reader.Read())
                        {
                            maxstockid =  int.Parse(reader[1].ToString());
                        }
                    }
                    finally
                    {
                        // Always call Close when done reading.
                        reader.Close();
                    }
                }
    Friday, April 29, 2016 1:10 PM

Answers

  • Your created a DataReadder

    SqlDataReader reader = command.ExecuteReader();

    not a "DataSet".

    Your code has try and where is the catch?

    In your case you might even hit error when you are opening the database.

    Structure you code try.. catch before final and try again.

    chanmm


    chanmm

    • Marked as answer by MrBrilliant12 Saturday, May 7, 2016 6:32 AM
    Sunday, May 1, 2016 7:23 AM

All replies

  • Please do not post duplicate threads to different (or even the same) forum.  Once is sufficient, especially if you chose the correct forum.

    duplicate

    And learn how to post useful information.  No one can read your mind or see your screen or has access to your "system".  If you encounter a problem, you need to be more specific than just stating that you have encountered a problem.  Statements like "... don't find my server while creating ..." don't provide any useful information.  Did an error occur?  Did something else happen?  Did the keyboard explode?  And don't try to combine different issues into the same post.  Since this post is focused on csharp, your duplicate is the more appropriate forum.

    Friday, April 29, 2016 1:25 PM
  • actually I said 2 problems, first: when I create dataset, I don't find my server while creating new connection.

    second: the SQL connection and SQL command I posted is not working, because it can't connect to the database.

    and my Question was if I can connect to the database through the code without creating new dataset or not.

    if yes, is there's any problems in my code to do this or not. I though I was clear.

    also I searched in many topics before adding this one, so don't be so offensive again

    Friday, April 29, 2016 1:52 PM
  • Your created a DataReadder

    SqlDataReader reader = command.ExecuteReader();

    not a "DataSet".

    Your code has try and where is the catch?

    In your case you might even hit error when you are opening the database.

    Structure you code try.. catch before final and try again.

    chanmm


    chanmm

    • Marked as answer by MrBrilliant12 Saturday, May 7, 2016 6:32 AM
    Sunday, May 1, 2016 7:23 AM