私はC#ASP.NETにいるので、これは初めてです。自分自身を教えようとしていますが、私は単純でなければならないと思っているものに夢中になっています。私は、私が探しているものを記述する適切な方法を知らないと思うので、ここで検索で答えを見つけることができませんでした。だから、私は最後に答えを君に盗聴することに頼った。複数の結果を持つデータリストです.SQLの挿入を行うテキストボックスとボタンがあります。
私は非常に基本的で、私は全く新しいですが熱心です。
データベース(MSSQL)からX個の結果を返すデータリストがあります。各結果にはいくつかの情報があり、次に2つのテキストボックスとボタンがあります。私は、各ボックスにいくつかの情報を入力してボタンをクリックして、それを私のSQLデータベースに戻すことができるようにしたい。
(私のインサートが、これから提示された結果かを知っているように)私は、ID(データリストの結果から返されたSQL値)に沿って各テキストボックスからテキスト結果は、それに行きたい
ので、私のページ男は、テキスト2の2つのテキストボックスに記入し、テキスト2のボタンをクリックした場合、私は」、(textbox1.text、textbox2.textを挿入などなど
- TEXTBOX - - TEXTBOX - BUTTON テキスト2
テキスト1のようになります。テキスト2 ")を私のデータベースに入れます
これはボタン2に、これまで私はクリック用の背後に自分のコードに持っているものcommandargument
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();
ある元データリストの行に返されたブログエントリのIDです。私がここで直面している問題は、これが最初に返された結果だけで、他のものは返されないということです。私はこれがコントロールリストの[0]の私の使用によるものだと認識していますが、これをどのように修正するか分かりません。 [clientid]は役に立たなかった。
ご協力いただきありがとうございます。
編集:いくつかのコードを追加私はユーザーの提案後に試した、私はこれが彼が私にしたいと思うものだと思うが、私はエラーを受け取っていると思う:オブジェクト参照はオブジェクトのインスタンスに設定されていない。
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();
}
を、それは私はあなたが言ったことを試みたが、取得していますOK – JohnnyM