Solved

SQLDataSource Insert to Uniqueidentifier field error

Posted on 2008-10-21
7
789 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

820 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