Solved

SQLDataSource Insert to Uniqueidentifier field error

Posted on 2008-10-21
7
791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

724 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