开发者

datalist with multiple results, with textboxes and buttons that i want to do sql inserts, how?

i'm in c# ASP.NET and I'm new to all this. trying to teach myself but I'm hung up on something that I suppose should be simple. I couldn't find the answer on here with searches because I guess I don't know the appropriate way to describe what I'm looking for. So i last resorted to bugging you guys for an answer.

Please be very basic, I'm quite new but eager.

I have a datalist returning X number of results from a database (MSSQL) - each result comes with some information, and then 2 textbox's and a button. i want them to be able to enter some information into each box, click a button, and then that is inserted back into my SQL database.

I want the text results from each textbox, along with the id (a sql value returned from the datalist's results) to go with it (so that my insert knows which results this is from)

so my page looks like

text 1 - TEXTBOX - TEXTBOX - BUTTON text 2 - etc etc

if the guy fills out text 2's 2 textboxes and clicks text2's button, i insert (textbox1.text, textbox2.text, "text 2")开发者_高级运维 into my db

this is what i have in my code behind for the click so far

SqlDataSource commentinsert = new SqlDataSource();
    commentinsert.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    commentinsert.InsertCommandType = SqlDataSourceCommandType.Text;
    commentinsert.InsertCommand = "INSERT INTO ocomments (cuser, date, ip, blogid, text) VALUES (@cuser, @date, @ip, @blogid, @text)";
    commentinsert.InsertParameters.Add("cuser", ((TextBox)DataList1.Controls[0].FindControl("TextBox2")).Text);
    commentinsert.InsertParameters.Add("date", DateTime.Now.ToString());
    commentinsert.InsertParameters.Add("ip", Request.UserHostAddress.ToString());
    commentinsert.InsertParameters.Add("blogid", ((Button)DataList1.Controls[0].FindControl("Button2")).CommandArgument.ToString());
    commentinsert.InsertParameters.Add("text", ((TextBox)DataList1.Controls[0].FindControl("TextBox3")).Text);
    commentinsert.Insert();

the commandargument in button2 is the ID of the blog entry as returned in original datalist rows. the problem I face here is that this only works for the first returned result and none of the others. I recognize this is due to my use of [0] in the controls list, but i have no idea how to fix this. [clientid] didn't help.

thanks in advance for any help guys.

EDIT: adding some code i tried after a user suggestion, i think this is what he wanted me to do but i'm receiving errors: Object reference not set to an instance of an object.

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    Button Button2b = (Button)e.Item.FindControl("Button2");
    TextBox TextBox2b = (TextBox)e.Item.FindControl("TextBox2");
    TextBox TextBox3b = (TextBox)e.Item.FindControl("TextBox3");

    SqlDataSource commentinsert = new SqlDataSource();
    commentinsert.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    commentinsert.InsertCommandType = SqlDataSourceCommandType.Text;
    commentinsert.InsertCommand = "INSERT INTO ocomments (cuser, date, ip, blogid, text) VALUES (@cuser, @date, @ip, @blogid, @text)";
    commentinsert.InsertParameters.Add("cuser", TextBox2b.Text);
    commentinsert.InsertParameters.Add("date", DateTime.Now.ToString());
    commentinsert.InsertParameters.Add("ip", Request.UserHostAddress.ToString());
    commentinsert.InsertParameters.Add("blogid", Button2b.CommandArgument.ToString());
    commentinsert.InsertParameters.Add("text", TextBox3b.Text);
    commentinsert.Insert();
}


You really want to use the ItemCommand event of the DataList to perform the update. Set the DataKeys property to the ID field of your DataRow, grab the DataKey value in the ItemCommand event so you know what row you're updating.

To get a reference to the server control's data, use e.Item.ItemIndex[e.SelectedIndex] to get a reference to the correct row. Inside the ItemCommand event, of course.


Take a look at this example:

http://www.c-sharpcorner.com/UploadFile/anjudidi/208062009050129AM/2.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜