Guys i have a grid and need to retrieve text inputs from and then insert to the the database . The grid looks like below
伙计们我有一个网格,需要从中检索文本输入,然后插入数据库。网格如下所示
From the above it permits user to pass as many rows to the database as he so desired. i use the method below.
从上面它允许用户根据需要将尽可能多的行传递给数据库。我使用下面的方法。
private void insert()
{
connection.Open();
for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
{
string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@row1", gvAdditionalDetails.Rows[i].Cells[1].Text.Trim());
cmd.Parameters.AddWithValue("@row2", gvAdditionalDetails.Rows[i].Cells[2].Text.Trim());
cmd.Parameters.AddWithValue("@contactID", 39);
cmd.ExecuteNonQuery();
}
connection.Close();
}
The above method loop throw my grid , how ever is returning null for the columns type which has a dropdown selection and description which has a tetxbox control. I cant call this controls invidually because they are declared in a grid. How do i retrieve the text the selected item from the dropdown and the inserted text from the textbox. the code gvAdditionalDetails.Rows[i].Cells[1].Text.Trim()
returns null.
上面的方法循环抛出我的网格,如何为具有下拉选择和具有tetxbox控件的描述的列类型返回null。我无法调用这个控件,因为它们是在网格中声明的。如何从下拉列表中检索所选项目的文本以及从文本框中插入的文本。代码gvAdditionalDetails.Rows [i] .Cells [1] .Text.Trim()返回null。
4 个解决方案
#1
3
You need to refer to the control inside the gridview row.
您需要引用gridview行内的控件。
private void insert()
{
connection.Open();
for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
{
string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@row1", ((DropdownList)gvAdditionalDetails.Rows[i].FindControl("DropDownListType")).SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@row2", ((TextBox)gvAdditionalDetails.Rows[i].FindControl("txtDescription")).Text.Trim());
cmd.Parameters.AddWithValue("@contactID", 39);
cmd.ExecuteNonQuery();
}
connection.Close();
}
#2
2
INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
#3
0
INSERT INTO Table (Column1, Column2)
select value1, value2
union all
select value1, value2
union all
select value1, value2
#4
0
Thanks all. I loop through and retrieve the inputs as follows . I am not sure it its the best way to do it , but it did work for me.
谢谢大家。我循环并检索输入如下。我不确定它是最好的方法,但它确实对我有用。
private void insert()
{
connection.Open();
for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
{
DropDownList DropDownListType =
(DropDownList)gvAdditionalDetails.Rows[i].Cells[1].FindControl("DropDownListType");
TextBox TextBoxDescription =
(TextBox)gvAdditionalDetails.Rows[i].Cells[i].FindControl("txtDescription");
string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@row1", DropDownListType.SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@row2", TextBoxDescription.Text.Trim());
cmd.Parameters.AddWithValue("@contactID", 39);
cmd.ExecuteNonQuery();
}
connection.Close();
}
#1
3
You need to refer to the control inside the gridview row.
您需要引用gridview行内的控件。
private void insert()
{
connection.Open();
for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
{
string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@row1", ((DropdownList)gvAdditionalDetails.Rows[i].FindControl("DropDownListType")).SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@row2", ((TextBox)gvAdditionalDetails.Rows[i].FindControl("txtDescription")).Text.Trim());
cmd.Parameters.AddWithValue("@contactID", 39);
cmd.ExecuteNonQuery();
}
connection.Close();
}
#2
2
INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
#3
0
INSERT INTO Table (Column1, Column2)
select value1, value2
union all
select value1, value2
union all
select value1, value2
#4
0
Thanks all. I loop through and retrieve the inputs as follows . I am not sure it its the best way to do it , but it did work for me.
谢谢大家。我循环并检索输入如下。我不确定它是最好的方法,但它确实对我有用。
private void insert()
{
connection.Open();
for(int i=0; i< gvAdditionalDetails.Rows.Count ; i++)
{
DropDownList DropDownListType =
(DropDownList)gvAdditionalDetails.Rows[i].Cells[1].FindControl("DropDownListType");
TextBox TextBoxDescription =
(TextBox)gvAdditionalDetails.Rows[i].Cells[i].FindControl("txtDescription");
string sql = "insert into [CONTACT_DETAILS] (type,description,contactID) VAlUES (@row1,@row2,@contactID )";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@row1", DropDownListType.SelectedItem.Text.Trim());
cmd.Parameters.AddWithValue("@row2", TextBoxDescription.Text.Trim());
cmd.Parameters.AddWithValue("@contactID", 39);
cmd.ExecuteNonQuery();
}
connection.Close();
}