1
votes

I have my database linked to my project and I am trying to view my database data in a data grid view. My database is written in sql server and My code is

          SqlCommand^ myCommand = gcnew SqlCommand("SELECT * FROM CSC 289.Customer WHERE Customer_ID = '" + grid + "';", myCon);
          SqlDataReader^ myReader;

            try {
                 myCon->Open();

                  myReader = myCommand -> ExecuteReader();

                  if (myReader -> Read()){
                      String^ ID = myReader -> GetString("Customer_ID");
                      txtID -> Text = ID;
                      /*String^ NameVal = myReader -> GetString("Customer_Name");
                      txtName -> Text = NameVal;
                      String^ PhoneVal = myReader -> GetString("Customer_Phone");
                      txtPhone -> Text = PhoneVal;
                      String^ EmailVal = myReader -> GetString("Customer_Email");
                      txtEmail -> Text = EmailVal;*/
                  }

             }
             catch (Exception^ ex) {
                 MessageBox::Show(ex->Message);
             }
         }

The errors I receive when I try and run my program are

error C2664: 'System::String ^System::Data::Common::DbDataReader::GetString(int)' : cannot convert parameter 1 from 'const char [12]' to 'int'

and

function "System::Data::SqlClient::SqlDataReader::GetString" cannot be called with the given argument list argument types are: (const char [12]) object type is: System::Data::SqlClient::SqlDataReader ^

I am not sure how to fix my problem and help would be great.

1

1 Answers

0
votes

As it says on the box GetString expects an Integer and returns a string. https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring(v=vs.110).aspx

SqlDataReader.GetString Method (Int32)

here is a good article on how to use a data reader in c++ http://www.digitalcoding.com/Code-Snippets/CPP-CLI/C-CLI-Code-Snippet-Get-Data-From-SqlDataReader.html

Correct syntax that you are looking for is C++

String^ ID = myReader["Customer_ID"]->ToString();

C# syntax

String ID = myReader["Customer_ID"].ToString();