0

So I'm trying to pass in parameters for point strings, all my columns are marked not null in my DB however when i try to run the below code it tells me column Lat cannot be null. Is there something i'm overlooking?

string latParameter = "'Point(" + latString + ")'";
            string lonParameter = "'Point(" + lonString + ")'";

            string mySQLfinishedProcessing = " insert into zipcodes " +
              "set zipcode = '" + zipcodeString + "'" +
              ",State = '" + StateString + "'" +
              ",City = '" + CityString + "'" +
              ",lat = GeomFromText(@latParam)"+
              ",lon = GeomFromText(@lonParam)"+
            ",StateCode = '" + StateCodeString2 + "'";

            //File.AppendAllText(@"C:\sqlupdater.txt", mySQLfinishedProcessing + ";\n");

            MySqlConnection configCON = new MySqlConnection(SQLStringClass.zipCONString);
            MySqlCommand CounterLogs = new MySqlCommand(mySQLfinishedProcessing, configCON);
            CounterLogs.Parameters.Add(new MySqlParameter("@latParam", latParameter));
            CounterLogs.Parameters.Add(new MySqlParameter("@lonParam", lonParameter));
            configCON.Open();
            CounterLogs.ExecuteNonQuery();
            configCON.Close();

yes, latString and lonString has values.

string latParameter = 'Point(40.92233)' string lonParameter = 'Point(-72.63708)'

okay it appears the be cause there is only a single reference to a point, i change string latParameter = 'Point(40.92233 12)' it takes just fine. But do I really want to put both my lat/lon in 1 point column?

ugh nevermind that works fine in MySQl Edit, but in C# it still doesnt resolve the issue. The problem is the string lonParameter needs to contain a ;

here is the correct code.

 string latParameter = "Point(" + latString + " " +lonString+");";

            string mySQLfinishedProcessing = " insert into zipcodes " +
              "set zipcode = '" + zipcodeString + "'" +
              ",State = '" + StateString + "'" +
              ",City = '" + CityString + "'" +
              ",Location = GeomFromText(@latParam)"+
            ",StateCode = '" + StateCodeString2 + "'";

            //File.AppendAllText(@"C:\sqlupdater.txt", mySQLfinishedProcessing + ";\n");

            MySqlConnection configCON = new MySqlConnection(SQLStringClass.zipCONString);
            MySqlCommand CounterLogs = new MySqlCommand(mySQLfinishedProcessing, configCON);
            CounterLogs.Parameters.Add(new MySqlParameter("@latParam", latParameter));
            configCON.Open();
            CounterLogs.ExecuteNonQuery();
            configCON.Close();
8
  • Well, what is the value of latString etc? Also, why aren't the other strings parameters? Come to mention it, why can't the call to Point() use parameters? Commented Jun 14, 2011 at 17:12
  • why do i need to make them all parameters? the other values are just int's and varchar? Commented Jun 14, 2011 at 17:13
  • @mike in your smysqlfinishedprocessingstring your lat = is lowercase maybe u need to do Lat = instead cause columns are case sensitive in SQL Commented Jun 14, 2011 at 17:13
  • @Mike actually I was thinking of SQL injection. concatenating values is very very dangerous. Commented Jun 14, 2011 at 17:14
  • No it's not a case sensitive issue Commented Jun 14, 2011 at 17:15

2 Answers 2

1

you are writing insert statment with wrong syntax. it must be

string mySQLfinishedProcessing = " insert into zipcodes(zipcode ,State ,City ,lat ,lon ,StateCode ) values ( " +
          "'" + zipcodeString + "'" +
          ",'" + StateString + "'" +
          ",'" + CityString + "'" +
          ",GeomFromText(@latParam)" +
          ",GeomFromText(@lonParam)" +
        ",'" + StateCodeString2 + "')";
Sign up to request clarification or add additional context in comments.

1 Comment

No that formatting is fine, i make these values allow null and everything inserts right, i do my inserts like this allt he time
0

If GeomFromText is a db function, it's probably returning null for whatever you're passing in.

Comments

Your Answer

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