开发者

Is possible to insert using sqldatasource without defaultvalue property?

Is possible to perform insert operation on sqldatasource in code-behind without using defualt value property? So开发者_JAVA技巧mething like provide some key-value collection for the parameters and use the Insert() method?

I'm asking because the InsertParameters[x].DefaultValue seems to be doing (in theory of course) something else - I guess redefinition of the markup and it's rather a workaround.

Do you know some other way to do this?


You have to use the Inserting event in which you will be forming the inserted values. After that you can use Insert() where you want.


While the DefaultValue method seems oddly named, it will actually work fine. Most likely there is an issue someplace else.

You also said you wanted to alter the control from code behind. You won't be able to directly put values in without using DefaultValue. In order to avoid that specific property, you'll need to create the object and fill it out.

That being said, you asked if you can use some sort of Key-Value pair to populate an insert. One way you could do this is something along the lines of

MyArray myArray = new MyArray();
myArray.add("FirstName", "John");
myArray.add("LastName", "Smith");
myArray.add("Address", "1234 Main St");
myArray.add("Phone", "123-456-786");

SqlDataSource sqlNames = New SqlDataSource(/* Conn */);

sqlNames.InsertCommand = "INSERT INTO [names] ([FirstName],[LastName],[Address],[Phone]) VALUES(@Fname, @Lname, @Address, @Phone)";
foreach (MyObject o in myArray) {
  // Assuming an array of MyObject which has two members
  sqlNames.InsertParameters.Add("@" + o.DBColumnName, o.Value);
}

sqlNames.Insert();

This is semi-psuedo code, but that's the general idea to do what you want. Use a HashTable, custom object array, etc.

Feel free to ask for more help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜