Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot convert to sqltypes

Status
Not open for further replies.

JohnnyLong

Programmer
Sep 27, 2002
97
GB
I've searched everywhere for the answer but to no avail. I have a SQL Insert class which accepts sql parameters as for example SqlDbType.NVarChar. I create a new instance of this class in my project but when I try oInsert.Name = txtName.Text i get the error "Value of type String cannot be converted to System.Data.SqlTypes.SqlString". I've Imported the SqlTypes namespace. How do I convert the string to SqlType or am I missing something crucial?

John.
 
System.Data.SqlTypes.SqlString has a .value property, which is where you should assign the string.

I'd suggest abstracting that logic into your property, where the set operation would assign to

System.Data.SqlTypes.SqlString.value = value

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Hi Paul,
Thanks for the reply, unfortunately I'm a little out of my depth at the moment. You passed me a link last week to the LLBLGEN class generator, which was great. I found another site which gives an example using C#. Following this example I've attempted to convert the code to vb7. The C# CreateNewEmployee void simply assigns the form text box values to the object eg oEmployees.FirstName = tbxEmployeeFirstname.Text; Can I not do this in vb7? or do I have to create a Get Set Public Property?

Any help greatly appreciated.
John.
 
I do see your point, and w/o playing with it (no time just now), all I can say is that you need to go look at the property that you're assigning to, and see the exact type that it's expecting.

There's about a hundred different things that could be going on there, although I can't imagine why the same assignment wouldn't work in vb7...

But I guess I wouldn't be terribly surprised, either. I'm really starting to lean towards C# for several different reasons, and if I had to give advice at this point as to which language to use, I'd certainly advise to go with C#.

But that's another discussion for a whole 'nuther thread.

If you still can't get things running, post back to this thread, and I'll take some time with it this evening to try to see what's going on there.

good luck -
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Johnny, what does the class you're working with look like? I just got around to inspecting this code, and I hate to say it, but it looks as though they dropped the ball here:
Code:
		public SqlString sFirstName
		{
			get
			{
				return m_sFirstName;
			}
			set
			{
				SqlString sFirstNameTmp = (SqlString)value;
				if(sFirstNameTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("sFirstName", "sFirstName can't be NULL");
				}
				m_sFirstName = value;
			}
		}
They test cast it to that temp variable, and then test that var to see if it's null, throw an exception if it is, and then turn right around and try to assign the input directly to the private variable w/o another cast.

If I re-wrote this, the last line would be:
Code:
m_sFirstName = sFirstNameTmp;

I hope that helps out.

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top