[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2144
  • Last Modified:

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>
0
ohmstor
Asked:
ohmstor
  • 4
  • 3
1 Solution
 
aki4uCommented:
You can NOT change uniqueIdentifier!
What type is userID?
0
 
aki4uCommented:
in your database ... I see you are using here an Object type for both??? Any reason for this?
0
 
aki4uCommented:
Sorry, I thought you are trying to change the Identity...never mind
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ohmstorAuthor 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.
0
 
bsdotnetCommented:
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"



0
 
aki4uCommented:
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.
0
 
ohmstorAuthor 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.
0
 
ohmstorAuthor Commented:
How can you add assisted answers?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now