How do we go about adding a row on the console to read in data for the new record

Please ponder on my question before answering as to how practically when we display a table that we could add a row in their to enter a new record? and how that row will get inserted after the error check.  You may answer my question by a practical example - thanks.  

I am using mssql and c#


         static void HasRows(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}
goodkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

goodkAuthor Commented:
sure!
0
Bob LearnedCommented:
What do you mean by "adding a row on the console "?  Are you looking to get user input, and then add a row to a DataTable?
0
goodkAuthor Commented:
Actually, I display the table wrapped in html Table.  

So you can show it like different rows of the table and the bottom row is to add a new record.

Really, asking how do we add a record to inquire of user input for the table when  there are no records? Also when there are records and we have add one new record.

All you have to do to answer my question is to say, at this point on the above loop you inquire for the new record. Or rewrite the loop to display the record and also ask for the new record,
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

goodkAuthor Commented:
while (reader.Read())
                        {
                           
                            htmlstr.Append("<TR>");
                            for (fcount = 0; fcount < reader.FieldCount; fcount++)
                            {


                                        htmlstr.Append("<TD " + styleTD + ">" + reader.GetName(fcount) + "</TD>");
                                    }
                                    htmlstr.Append("</TR>");



                        }  //whilefor count loop ends
0
goodkAuthor Commented:
htmlstr = new StringBuilder("<table>");
        static void HasRows(SqlConnection connection)
{
    using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            htmlstr.Append("<TR>");
            while (reader.Read())
                        {
                           
                            htmlstr.Append("<TR>");
                            for (fcount = 0; fcount < reader.FieldCount; fcount++)
                            {


                                        htmlstr.Append("<TD " + styleTD + ">" + reader.GetName(fcount) + "</TD>");
                                    }
                                    htmlstr.Append("</TR>");



                        }  //whilefor count loop ends
        }
 return htmlstr.ToString() + "</Table>";
        else
        {
           Response.write("No rows found.");
        }
        reader.Close();
    }
}

What is the best place to add for a function to ask the user for the contents for the new record?  I do understand that I have to include html <input card and I also understand that I have to add some sort of Ajax to do the error check.

All my puzzle is to resolve that if there are no rows then how do I get into the Read loop?  Same when I run out of contents then how do i add a row to ask for contents for the new record from the user?
0
Bob LearnedCommented:
That sounds like you need to find a place to put in <input> elements, and a button to submit a post back.
0
goodkAuthor Commented:
All I am asking is where in the loop?
0
Bob LearnedCommented:
I still don't know what you are looking for, so that makes it difficult to suggest the better course of action.  You are creating rows with <tr> elements, and cells with <td> elements.  Any HTML elements should go inside of the <td> elements, so a line like this

 htmlstr.Append("<TD " + styleTD + ">" + reader.GetName(fcount) + "</TD>");

where HTML can be inserted between the beginning <td> and the ending </td>.
0
goodkAuthor Commented:
Just do an example where you display content of a table and a row to insert new record- typical

Then suggest how to write the code, perhaps what I already have and add few lines to create the input for the new record
0
goodkAuthor Commented:
how do you design to enter a new record? trying to know the entire cycle.

1- create input fields
2- validate all individual fields
3- check for each field to be acceptable with respect to each other
4- commit and add a new record
0
goodkAuthor Commented:
It is frustrating that I am not getting an answer to a question which is probably faced by every experienced programmer.
0
Bob LearnedCommented:
I certainly don't approach any problem like this, but try this on for size:

 for (fcount = 0; fcount < reader.FieldCount; fcount++)
                            {


                                        htmlstr.Append("<TD " + styleTD + "><input name='" + reader.GetName(fcount) + ""'></TD>");
                                    }
                                    htmlstr.Append("</TR>");



                        }  /
0
goodkAuthor Commented:
All I am asking is what is the best way to seek user input and do error check and insert a new record.

Something which is very typical and I am not sure why I cannot get a clear answer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
goodkAuthor Commented:
This question can be closed as I have not received a reasonable answer on my question for a long time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.