简体   繁体   中英

Issue in using Code in C# interact with mongodb

My Code is following :

            try
        {
            MongoDatabase mtest1 = mongoServer.GetDatabase("ews", mC);
            MongoCollection<EliteGuard> ecollection1 = mtest1.GetCollection<EliteGuard>("EliteGuard");

            int intC = FindUser(comboBox1.Text.ToString());
            int intCount = 0;
            foreach (EliteGuard t in ecollection1.FindAll())
            {
                if (t.product_key.Equals(comboBox1.Text.ToString()))
                {
                    intCount++;
                }
            }
            if (intC <= intCount)
            {
                MessageBox.Show("Total no. of Serial Key is generated.", "Elite Manager Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        catch (MongoConnectionException mcex)
        {
            MessageBox.Show("Unable to connect to Server. Please try Again.", "Elite Manager Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        catch (Exception ex)
        {
            return;
        }

Error :

An error occurred while deserializing the product_key property of class Serial_Key_Generation.Form5+EliteGuard: Input string was not in a correct format.

StackTrace : at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMember(BsonReader bsonReader, Object obj, BsonMemberMap memberMap) at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) at MongoDB.Driver.Internal.MongoReplyMessage 1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) at MongoDB.Driver.MongoCursorEnumerator 1.GetReply(MongoConnection connection, MongoRequestMessage message) at MongoDB.Driver.MongoCursorEnumer ator 1.GetFirst() at MongoDB.Driver.MongoCursorEnumerator 1.MoveNext() at Serial_Key_Generation.Form5.comboBox1_SelectedIndexChanged(Object sender, EventArgs e) in D:\\Projects\\Serial Key Generation\\Serial Key Generation\\Form5.cs:line 94

But the same type of code is working properly on another Form.

An error occurred while deserializing the product_key property of class Serial_Key_Generation.Form5+EliteGuard: Input string was not in a correct format.

There is an inconsistency between the implementation of the EliteGuard class and your database data. The product_key property in that class does not match its equivalent in the database. It's possible you defined it as the wrong data type in your class definition. It seems to be expecting a string but it's not getting one. You'll need to change the data type of product_key either in the class definition or in the DB (class definition is probably easier).

But the same type of code is working properly on another Form.

That doesn't really tell me much. Are you loading data from the same DB & collection and are you using the same class? It's hard to answer this without more information. In any case it sounds like something is different, because it's not having trouble deserializing there. Try stepping through the code in each scenario and see what is different from one form to the other. It may just be a small difference that you overlooked. Having a separate working example should make it easier to see what is wrong with the form that doesn't work.

是的,它连接相同的数据库,并且来自另一个表单的代码相同。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM