Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using the code below (VB) to insert things into an SQL database. I want to use this code on a C# page. How can I do this?

 

           	Dim MyCommand As New SqlCommand
		Dim MyConnection As New SqlConnection
		Dim MySQLDataAdapter as New SqlDataAdapter
		Dim SessionCmd As String

		SessionCmd = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" & user & "', '" & savename.Value & "', 0)"
		
		MyConnection = New SqlConnection("server=(local);database=PictureShare;Trusted_Connection=yes")
						
		MyCommand = New SqlCommand(SessionCmd, MyConnection)

		MyCommand.Connection.Open()
			
			Try
				MyCommand.ExecuteNonQuery()
			Catch Exp As SQLException
				If Exp.Number = 2627
		            
				Else
		            
				End If
			End Try
			
		MyCommand.Connection.Close()

Thanks,

Tehon

  • Moderators
Posted

try this...

 

MyConnection = new SqlConnection("server=127.0.0.1;database=PictureShare;Trusted_Connection=yes");
MySQLDataAdapter = new SqlDataAdapter();
SessionCmd String = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" & user & "', '" & savename.Value & "', 0)";
MyCommand = new SqlCommand(SessionCmd, MyConnection);

MyCommand.Connection.Open();
   
try
{
MyCommand.ExecuteNonQuery();
}
catch(SQLException Exp)
{
if (Exp.Number == 2627)
{

}
else
{
           
}
}
MyCommand.Connection.Close();

Visit...Bassic Software
  • *Experts*
Posted

If you want to use VB code in a C# project without having to

convert it all, you could compile the VB code into a class library

project that you can reference from your C# project. Then

you can use all the methods and properties in it just like any class.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted
I used that code and it tells me that MyConnection is not declared. I'm importing the same things for SQL in VB and C#. Do I need to import something else?

Thanks,

Tehon

  • *Experts*
Posted

The declaration for these variables should be as follows:

 

SqlConnection MyConnection = new SqlConnection("server=127.0.0.1;database=PictureShare;Trusted_Connection=yes");
SqlDataAdapter MySQLDataAdapter = new SqlDataAdapter();
string SessionCmd = "INSERT INTO tmPicIndex (UserID, PicName, PicDesc) VALUES ('" + user + "', '" + savename.Value + "', 0)";
SqlCommand MyCommand = new SqlCommand(SessionCmd, MyConnection);

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...