1

Currently I'm doing an insert into the db from codebehind with a sqldatasource in the following way.

SqlDataSource13.InsertParameters["name"].DefaultValue = ufn;
            SqlDataSource13.InsertParameters["mime_type"].DefaultValue = FileUpload1.PostedFile.ContentType;
            SqlDataSource13.InsertParameters["size"].DefaultValue = FileUpload1.PostedFile.ContentLength.ToString();
            SqlDataSource13.InsertParameters["extension"].DefaultValue = "";
            SqlDataSource13.InsertParameters["date"].DefaultValue = dt.ToShortDateString();
            SqlDataSource13.InsertParameters["type"].DefaultValue = ddlDocType.SelectedValue;
            SqlDataSource13.InsertParameters["description"].DefaultValue = tbDescription.Text;
            SqlDataSource13.Insert();

This is working fine. However I need to now get the insert id. I added select scope_identity to the end of the insert query. and the parameter to the insertparameters with direction=ouput.

SELECT @id = SCOPE_IDENTITY()
<asp:Parameter Name="id" Direction="Output" />

however I need to figure out how to get the output parameter with the setup i have above.

2 Answers 2

1

Try this:

protected void SqlDataSource13_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    string sID = e.Command.Parameters["@Identity"].Value.ToString();
}

Taken from here (not C# but the idea is there)

Sign up to request clarification or add additional context in comments.

1 Comment

I knew about that one. however I wanted to use it with what I was doing above. In the end I changed to a linq query.
0

In order to make things easier on myself I switched to a linq to sql query.

tableDataContext tdc = new tableDataContext();
doc d = new doc();
d.dpi = qs;
d.dn = ufn;
etc...
tdc.docs.InsertOnSubmit(d);
tdc.SubmitChanges();

// get current row insert id
int dID = d.id;

So now I have the recently inserted doc id to use. Easy.

Thanks to whoever wrote this example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.