We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

SqlDataSource InsertParameters C# .Net 2.0

ohmstor
ohmstor asked
on
Medium Priority
2,165 Views
Last Modified: 2008-03-06
Hello Experts,

I am using C# and ASP.Net 2.0 to store values in a database. I am using a SqlDataSource to connect to my database. I am able to retrieve data however I have problems storing data. I have the datasource set to use datasets.

The problem I am facing is that to store an entry I need two parameters of type Guid. However when I try to insertParameters using Guids I get an error saying that I need to use Strings. However when I use Strings I get an exception when inserting saying that I need a uniqueIdentifier and not a String.

Please give me advice on how to store a new entry or to set the parameters correctly. I can't use databinding for this task.

Thank You,
Ohmstor

Below is some code that I use:
           
            Guid schedulerId = new Guid();
            Guid userId= new Guid();
       
            SqlDataSource2.InsertParameters["schedulerID"] = schedulerId;
            SqlDataSource2.InsertParameters["userID"] = userId;
           
            SqlDataSource2.Insert();
--------------------------------------
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyLocalSQLServer %>"
        DeleteCommand="DELETE FROM [scheduler] WHERE [schedulerID] = @schedulerID" InsertCommand="INSERT INTO [scheduler] ([schedulerID], [userID]) VALUES (@schedulerID, @userID)"
        ProviderName="<%$ ConnectionStrings:MyLocalSQLServer.ProviderName %>" SelectCommand="SELECT [schedulerID], [userID] FROM [scheduler]"
        UpdateCommand="UPDATE [scheduler] SET [userID] = @userID WHERE [schedulerID] = @schedulerID">
        <DeleteParameters>
            <asp:Parameter Name="schedulerID" Type=Object />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="userID" Type=Object />
            <asp:Parameter Name="schedulerID" Type=Object />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="schedulerID" Type=Object />
            <asp:Parameter Name="userID" Type=Object />
        </InsertParameters>
    </asp:SqlDataSource>
Comment
Watch Question

Commented:
You can NOT change uniqueIdentifier!
What type is userID?

Commented:
in your database ... I see you are using here an Object type for both??? Any reason for this?

Commented:
Sorry, I thought you are trying to change the Identity...never mind

Author

Commented:
both the userId and the schedulerId are uniqueIdentifers.

I am using the Object as a Type because I couldn't find Guid or UniqueIdentifer and it seems to be the closest match.

Commented:
you can use NEWID() as default value for UserID and SchedulerID in your SQL table design.

NEWID() function to will give you a globally unique ID (GUID).

"Guid schedulerId = new Guid();" and "Guid userId= new Guid();" give you "00000000-0000-0000-0000-000000000000"



Commented:
you ca use that or from your code behind, like this:
string strGuid = Guid.NewGuid().ToString();

so, to get new guid use: Guid.NewGuid().ToString()

also, I would change object type you have to string.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanx a lot! When I changed the object type to string in the SqlDataSource it worked. Can you guys explain why though? Also thanx for providing the newguid function.

Author

Commented:
How can you add assisted answers?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.