Link to home
Start Free TrialLog in
Avatar of ohmstor
ohmstor

asked on

SqlDataSource InsertParameters C# .Net 2.0

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>
Avatar of aki4u
aki4u

You can NOT change uniqueIdentifier!
What type is userID?
in your database ... I see you are using here an Object type for both??? Any reason for this?
Sorry, I thought you are trying to change the Identity...never mind
Avatar of ohmstor

ASKER

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.
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"



ASKER CERTIFIED SOLUTION
Avatar of aki4u
aki4u

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ohmstor

ASKER

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.
Avatar of ohmstor

ASKER

How can you add assisted answers?