0
嗨我有一些方法,從sql表發送我的數據,我希望 - 當我點擊datagridview按鈕 - 發送參數的方法和參數將有ID值(如圖片107或106)。圖片下面是帶有2個按鈕和ID列的datagridview。按鈕發送參數的方法
public ObservableCollection<MyClass> ReadUpdate(int id_update)
{
ObservableCollection<MyClass> result = new ObservableCollection<MyClass>();
string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataReader dr;
SqlConnection conn = new SqlConnection(nwConn);
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.CommandText = "Insert_Update";
cmd.Parameters.AddWithValue("@id_update", id_update);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
MyClass lin = new MyClass();
lin.id = dr.GetInt32(1);
if (!dr.IsDBNull(2)) lin.other = dr.GetString(2);
if (!dr.IsDBNull(3)) lin.barkod = dr.GetString(3);
if (!dr.IsDBNull(4)) lin.pw = dr.GetInt32(4);
result.Add(lin);
}
dr.Close();
return result;
}
catch (SqlException e)
{
MyClass lin = new MyClass();
lin.other = e.Message;
result.Add(lin);
return result;
}
finally
{
conn.Close();
};
}
我的類:
public class PIS
{
public int ID { get; set; }
}
我的按鈕:
private void btnUpdate_Click(object sender, System.Windows.RoutedEventArgs e)
{
pis_update = (PIS)((Button)sender).DataContext;
ChildWindow_Update childWindow_update = new ChildWindow_Update();
childWindow_update.DataContext = ((Pismo)((Button)sender).DataContext).Id_Pismo;
childWindow_update.Closed += ChildWindow_Update_Closed;
childWindow_update.Show();
}
public ChildWindow_Update()
{
InitializeComponent();
ServiceReference1.Service1Client webService = new ServiceReference1.Service1Client();
webService.ReadUpdateAsync((int)this.DataContext);
webService.ReadUpdateCompleted += WebService_ReadUpdateCompleted;
private void WebService_ReadUpdateCompleted(object sender, ServiceReference1.ReadUpdateCompletedEventArgs e)
{
if (e.Result != null)
{
//do something
}
}
我有錯誤webService.ReadUpdateAsync((INT)this.DataContext); 「空引用異常」。
請[讀這個SO關於NullReferenceException的問題](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)。 –