2

How can I bind data in xaml? I'm using the following logic:

SqlConnection conn = new SqlConnection("Data Source=mahendra;Initial Catalog=Win8App;User ID=sa;Password=*******");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tbl_Registration", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
sda.Fill(dt);
cmd.ExecuteNonQuery();      
dataGrid1.ItemsSource = dt.DefaultView;
conn.Close();

My basic need is to show the same data as the gridview.

0

2 Answers 2

1

It would make sense to use some ORM software rather than direct usage of SqlConnection/SqlCommand, which are, by the way, not disposed in your code. Consider reading this article: http://msdn.microsoft.com/en-us/library/ee340709.aspx , there is an example on data-binding in WPF using Entity Framework.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have to execute the command. The DataAdapter do it for you (in the 'Fill' method). To show your result you can use your DataTable as a source and set the Autopopulate colums to true in your DataGrid. I think you don't need the DataSet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.