Ok-
I'm new at this, but just found out that to get data off a form and insert into SQL I can scrape it off the form and insert it in the <insertParameter> section by using 
<asp:ControlParameterName="text2"Type="String"ControlID="TextBox2"PropertyName="Text"> (Thanks CSharpSean)
Now I ALSO need to set the user name in the same insert statement. I put in a UserName control that is populated when a signed in user shows up on the page. But in my code I've tried:
<asp:ControlParameter Name="UserName" Type="String" ControlID="LoginName1" DefaultValue="Daniel" PropertyName="Text"/>
and I get teh error
DataBinding: 'System.Web.UI.WebControls.LoginName' does not contain a property with the name 'Text'.
So I take PropertyName="Text" out, and get the error:
PropertyName must be set to a valid property name of the control named 'LoginName1' in ControlParameter 'UserName'.
what is the proper property value?
SO....BIG question...
Is there a clean way to pass UserName to the insert parameters? Or ANY value for that matter? Id like to know how to write somehting like
String s_test = "test string";
then in the updateparameter part of the sqldatasource pass SOMEHITNG like (in bold) <asp:Parameter Name="UserName" Type="String"Value=s_test/>
Thanks in advance...again!
Dan
Create the parameter, with a name attribute and a type. Then in SqlDataSource_Inserting event, just set the value of the parameter to whatever you want it to be. I believe the syntax (VB.NET) is either
e.Command.Parameters("@.UserName").Value=s_test
or
e.InsertCommand.Parameters("@.UserName").Value=s_test
|||Sounds easy.
Im writing in c#
But Im not sure where you are suggesting dropping in the code?
Is it in the '<InsertParameters> of the sql data source?
What is the 'E' in the e.insert...
Sorry for being so dense, but can you give me a psudo-code example?
Thanks for the help!
Dan
 
Step by step:
"Create the parameter, with a name attribute and a type." This is what you did before, just add:
<asp:Parameter name="UserName" type="String" /> in the <InsertParameters> section of the sqldatasource control.
"Then in SqlDataSource_Inserting event": double click the sqldatasource control while in design mode.
That should take you to your code behind and create a dummy event called "SqlDatasource1_Selecting".
Now at the top of the window, there are two dropdown list boxes. The right one should say "Selecting", change it to "Inserting". Now you should have a dummy event sub called "SqlDataSource1_Inserting". The code I gave goes in there. "e" is the second parameter of the two that gets passed in whenever the sqldatasource control is about to do an insert.
If you were doing this in VB.Net, your code behind page (Mypage.aspx.vb) would have these two new subs:
ProtectedSub SqlDataSource1_Inserting(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)Handles SqlDataSource1.Inserting
e.Command.Parameters("@.UserName").Value=s_testEndSub
ProtectedSub SqlDataSource1_Selecting(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)Handles SqlDataSource1.SelectingEndSub
You can now delete the "SqlDataSource1_Selecting" sub if you want, since you aren't really using it.
|||Thanks a BUNCH!
(Will try it after i make MORE coffee...)
|||
C# People can use the above discussion and use
    protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        String var1 = TextBox1.Text;
        e.Command.Parameters["@.text1"].Value = var1;
    }
in the  inserting function
 
No comments:
Post a Comment