Solved

SQLDataSource Insert to Uniqueidentifier field error

Posted on 2008-10-21
7
788 Views
Last Modified: 2012-05-05
Hi,

I have been given a database to build a front end to.

I have a Company table that is a lookup table, and the ID of each record is a UniqueIdentifier object.  The table I am inserting into has a reference to the company table, and that column is also a uniqueidentifier

For the front end I have a SQLDataSource insert method that is called by clicking a button.

When it goes to run the Insert() function, I get the error: No mapping exists from DbType Object to a known OdbcType.

I've searched for this on Google and it tells me that my SELECT querystring is incorrect.  But all it is is "SELECT * FROM [view]"

When run the select query, it returns all data OK
/*C# code*/
    protected void lkbAddTariff_Click(object sender, EventArgs e)
    {
        DropDownList dCompany = gvTariff.FooterRow.FindControl("dlCompany") as DropDownList;
        TextBox tTarName = gvTariff.FooterRow.FindControl("txtTarName") as TextBox;
        TextBox tThreshold = gvTariff.FooterRow.FindControl("txtThreshold") as TextBox;
        TextBox tDirection = gvTariff.FooterRow.FindControl("txtDirection") as TextBox;
        TextBox tInaTar = gvTariff.FooterRow.FindControl("txtInaTariff") as TextBox;
        TextBox tCrossband = gvTariff.FooterRow.FindControl("txtCrossband") as TextBox;
 
        dsTariff.InsertParameters["ta_name"].DefaultValue = tTarName.Text;
        dsTariff.InsertParameters["ta_crossband"].DefaultValue = tCrossband.Text;
        dsTariff.InsertParameters["ta_thresholdDays"].DefaultValue = tThreshold.Text;
        dsTariff.InsertParameters["CompanyID"].DefaultValue = dCompany.SelectedValue;
        dsTariff.InsertParameters["INA_Tariff"].DefaultValue = tInaTar.Text;
        dsTariff.InsertParameters["Direction"].DefaultValue = tDirection.Text;
 
 
        dsTariff.Insert();
        gvTariff.DataSourceID = "dsTariff";
 
        gvTariff.DataBind();
    }
 
 
 
ASP.NET Code
 
 
 
<asp:SqlDataSource ID="dsTariff" runat="server" ConnectionString="<%$ ConnectionStrings:iceConnectionString %>" 
    InsertCommand="INSERT INTO [tbl1] ([ta_name], [ta_crossband], [ta_thresholdDays], [CompanyID], [INA_Tariff], [Direction]) VALUES (?, ?, ?, ?, ?, ?)" OldValuesParameterFormatString="original_{0}" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT * FROM [vwTbl1]" >
        <InsertParameters>
            <asp:Parameter Name="ta_name" Type="String" />
            <asp:Parameter Name="ta_crossband" Type="Int32" />
            <asp:Parameter Name="ta_thresholdDays" Type="Int32" />
            <asp:Parameter Name="CompanyID" Type="Object" />
            <asp:Parameter Name="INA_Tariff" Type="Int32" />
            <asp:Parameter Name="Direction" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

Open in new window

0
Comment
Question by:krapvag
7 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22765652
try replacing the select * with the actual list of columns
0
 
LVL 1

Author Comment

by:krapvag
ID: 22765686
Tried that, same result
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 22786950
Try qualifying it a little more:

"SELECT * FROM [dbo].[vwTbl1]"

"SELECT * FROM [DBname].[dbo].[vwTbl1]"

"SELECT * FROM [Server\instance].[DBname].[dbo].[vwTbl1]"
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22788330
Try eliminating the CompanyID parameter from your insert command completely.  I think the DB wants to provide that.
0
 
LVL 1

Author Comment

by:krapvag
ID: 22797933
Hi All,

Thank you all for your suggestions. Unfortunately I still could not add data to this field.  It is a Foreign Key field, so we should already have the uniqueID (therefore we cannot simply do NewID())

0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22798536
The underlying DB data type is UniqueIdentifier, right?

Would you switch your parameter from type Object to Guid ?
http://msdn.microsoft.com/en-us/library/cc668759.aspx
0
 
LVL 1

Author Closing Comment

by:krapvag
ID: 31508194
Yes, that seems to do it

Thanks a lot
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question