Saturday, February 25, 2012

programatically get values from sqldatasource

Hi All,

I am having problems getting values out of an sqldatasource.

I have 2 on the page, i am using one to insert into a table and the other to get a couple of values from another table for the insert.

the datasource i am trying to get the values from does a select with a querystring filter i need to grab a couple of fields out of it trhen do the insert on the other datasiource with a button click.

The insert is fine, i just don't know how to get the values out of the first 'select source'

Any pointers or suggestions most appreciated.

Cheers

I have figured this out.

I am doing the whole lot in the page load event handler and assigning the values to sessions then inserting the sesion variables in the iteminserting event handler for the formview

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

Dim dvAs DataView = _

CType(get_LocData.Select(DataSourceSelectArguments.Empty), DataView)

ForEach drAs DataRowIn dv.Table.Rows

Session("LocMan") = dr("LocManID").ToString()

Session("LocID") = dr("ID").ToString()

Session("Status") ="New"

Next

EndSub

ProtectedSub FormView1_ItemInserting(ByVal SenderAsObject,ByVal eAs FormViewInsertEventArgs)Handles FormView1.ItemInserting

e.Values("LocMan") = (Session("LocMan"))

e.Values("Status") ="new"

e.Values("LocationID") = (Session("LocID"))

EndSub

|||

Don't use an SqlDataSource for that.

using (SqlConnection conn = new SqlConnection("your connectionstring"))
{
SqlCommand cmd = new SqlCommand("SELECT ... FROM ... WHERE someColumn = @.someParameter", conn);
cmd.Parameters.AddWithValue("@.someParameter", Request.QueryString["yaddayadda"]);
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
col1=reader["col1"]; //Here you need to cast the value to the proper type, or use e.g reader.GetString(0) if it's a string
col2=reader["col2"];
}
}
}

But if you're selecting values just to insert them, you should be able to do it without retrieving the values in the first place. Please give us more details.

No comments:

Post a Comment