gridview中的选定行使用来自initial的参数填充另一个gridview [英] A selected row in a gridview populates another gridview with parameters from initial
本文介绍了gridview中的选定行使用来自initial的参数填充另一个gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误消息
Error Message
Object reference not set to an instance of an object.
左右
around
string intno4, intno5;
如何解决此问题?
< br $>
谢谢
How do I resolve this ?
Thanks
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//GRIDVIEW BEGIN
//txtSave.Enabled = false;
try
{
//connection to the database
//
//
string str;
str = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(str);
string intno,intno2;
string intno4, intno5;
//Get the intno
intno = GridView1.SelectedRow.Cells[1].Text;
intno2 = GridView1.SelectedRow.Cells[4].Text;
//call the stored procedure
SqlCommand SqlCmd = new SqlCommand("sp_Getleavactid", sqlcon);
SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
//Supply the User_id parameter
SqlCmd.Parameters.AddWithValue("@BRNNO", intno);
SqlCmd.Parameters.AddWithValue("@BYEAR", intno2);
//Create and supply the output parameters
SqlCmd.Parameters.Add("@BEMPNO", System.Data.SqlDbType.VarChar, 50);
SqlCmd.Parameters["@BEMPNO"].Direction = System.Data.ParameterDirection.Output;
//Open the sql data connection
sqlcon.Open();
//Execute the stored procedures
SqlCmd.ExecuteNonQuery();
//Assign the results to the controls
txt_Code.Text = SqlCmd.Parameters["@BRNNO"].Value.ToString();
txt_Year.Text = SqlCmd.Parameters["@BYEAR"].Value.ToString();
lblstatus.Text = "Grid record successful";
//
//
sqlcon.Close();
///
///
///GRIDVIEW2
///
///
string str1;
str1 = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
SqlConnection sqlcon1 = new SqlConnection(str1);
string intno4, intno5;
//Get the intno
intno4 = GridView2.SelectedRow.Cells[1].Text;
intno5 = GridView2.SelectedRow.Cells[4].Text;
//call the stored procedure
SqlCommand SqlCmd1 = new SqlCommand("sp_GetLeavGrid2id", sqlcon1);
SqlCmd1.CommandType = System.Data.CommandType.StoredProcedure;
//Supply the User_id parameter
SqlCmd1.Parameters.AddWithValue("@BRNNO", intno4);
SqlCmd1.Parameters.AddWithValue("@BYEAR", intno5);
//Create and supply the output parameters
//Create and supply the output parameters
SqlCmd1.Parameters.Add("@BEMPNO", System.Data.SqlDbType.VarChar, 50);
SqlCmd1.Parameters["@BEMPNO"].Direction = System.Data.ParameterDirection.Output;
//Open the sql data connection
sqlcon1.Open();
//Execute the stored procedures
SqlCmd1.ExecuteNonQuery();
//Assign the results to the controls
txt_Code.Text = SqlCmd1.Parameters["@BRNNO"].Value.ToString();
txt_Year.Text = SqlCmd1.Parameters["@BYEAR"].Value.ToString();
// txt_Ed_Date.Text = SqlCmd.Parameters["@BEDDATE"].Value.ToString();
lblstatus.Text = "Grid2View record successful";
//
//
sqlcon1.Close();
///
///
///GRIDVIEW2
///
///
}
catch (Exception ex)
{
lblstatus.Text = ex.Message;
}
//GRIDVIEW END
}
我的尝试:
试图调试无济于事。这是一个具体的例子
What I have tried:
Tried to debug to no avail. This is a specific example
推荐答案
它不能是你指示的线 - 正如Karthik Bangalore指出的那样。
它最有可能是类似
It cannot be the line you are indicating - as Karthik Bangalore has pointed out.
It is most likely to be one of the lines similar to
intno4 = GridView2.SelectedRow.Cells[1].Text;
如果Cells [1]为空,则.Text调用将生成您遇到的错误。同样,如果在GridView上没有选择行,您将得到相同的错误
If Cells[1] is empty then the .Text call will generate the error you are experiencing. Similarly if there is no row selected on the GridView you will get the same error
这篇关于gridview中的选定行使用来自initial的参数填充另一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文