0

There is a parameter in InsertParameters. When I try to set it I get an error::

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query

Here is my code in c#:

String complexID = Guid.NewGuid().ToString();
SqlDataSource1.InsertParameters["PKComplexID"].DefaultValue = complexID;

Can anybody help?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Karadous
  • 1,555
  • 3
  • 26
  • 37

5 Answers5

1

You need to change the SqlDbType on Inserting:

protected void SqlDataSource1_Inserting(object sender, 
  SqlDataSourceCommandEventArgs e) { 
    SqlParameter insertedKey = 
      e.Command.Parameters["@PKComplexID"] as SqlParameter; 
    insertedKey.SqlDbType = SqlDbType.UniqueIdentifier; 
}

References

http://forums.asp.net/t/1712791.aspx/1:

Object is incorrect type for uniqueidentifier field. But unfortunately we do not have uniqueidentifier in VisualStudio designer. So its giving you type Object. Neither you can make it uniqueidentifier which throws you exception. So you have to change this parameter type form code behind file in dataSource_Inserting event. Be sure its Inserting event. There you can get any parameter and set the uniqueidentifier fields type to uniqueidentifier.

GUIDs and DataSource Controls

zimdanen
  • 5,508
  • 7
  • 44
  • 89
1

I haven't tried this yet, but my first reaction is:

Guid complexID = Guid.NewGuid();
SqlDataSource1.InsertParameters["PKComplexID"].DefaultValue = complexID;
jaressloo
  • 210
  • 3
  • 13
0

You should not convert to string. Fix your code like this:

Guid complexID = Guid.NewGuid(); 
Dima
  • 1,717
  • 15
  • 34
0

I just changed the parameter type from object to string. it is solved.

Karadous
  • 1,555
  • 3
  • 26
  • 37
0

Try this one

 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Guid id=Guid.NewGuid();
FileUpload1.SaveAs(Server>MapPath("Your Path"+id+fileName ));
chandu
  • 2,276
  • 3
  • 20
  • 35
Imran
  • 1