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.InsertParam eters["sch edulerID"] = schedulerId;
SqlDataSource2.InsertParam eters["use rID"] = userId;
SqlDataSource2.Insert();
-------------------------- ---------- --
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyLocalS QLServer %>"
DeleteCommand="DELETE FROM [scheduler] WHERE [schedulerID] = @schedulerID" InsertCommand="INSERT INTO [scheduler] ([schedulerID], [userID]) VALUES (@schedulerID, @userID)"
ProviderName="<%$ ConnectionStrings:MyLocalS QLServer.P roviderNam e %>" 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>
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.InsertParam
SqlDataSource2.InsertParam
SqlDataSource2.Insert();
--------------------------
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyLocalS
DeleteCommand="DELETE FROM [scheduler] WHERE [schedulerID] = @schedulerID" InsertCommand="INSERT INTO [scheduler] ([schedulerID], [userID]) VALUES (@schedulerID, @userID)"
ProviderName="<%$ ConnectionStrings:MyLocalS
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>
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
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.
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-0 0000000000 0"
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-0
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
How can you add assisted answers?
What type is userID?