Skip to main content

Posts

Showing posts from January, 2013

How to Bind the DataGridView with DataSet and DataTable

We can bind the DataGridView in C#.net with the help of following code:        SqlConnection vConn = new SqlConnection("Server=localhost; database=rohit; user id=sa; pwd=***");             vConn.Open();             String vQuery = textBox1.Text;             SqlDataAdapter vAdap = new SqlDataAdapter(vQuery, vConn);             DataSet vDs = new DataSet();             vAdap.Fill(vDs, "Emp");             dataGridView1.DataBindings.Clear();             DataTable vDt = vDs.Tables["Emp"];             dataGridView1.DataSource = vDs;             dataGridView1.DataMember = "Emp";             vConn.Close(); //BIND THE LISTBOX WITH THE COLUMN FROM DATABASE. SqlConnection vConn = new SqlConnection( "Server=localhost; database=rohit; user id=sa; pwd=***" );             vConn.Open();             String vQuery = comboBox1.SelectedItem.ToString();             SqlDataAdapter vAdap = new SqlDataAdapter( "Select ename,

How to call Stored Procedure and use of ExecuteReader and Excecute Scalar

The following code demonstrates few things in C#.NET: //CALLING THE STORED PROCEDURE IN SQL SERVER SqlConnection conn = new SqlConnection ( "Server=(local);DataBase=Rohit;Integrated Security=SSPI" );             conn.Open();             int vEmpno;             String vEname;             int vSalary;             //insert command             //Pick all the values from the textboxes             vEmpno = Convert .ToInt32(textBox1.Text);             vEname = textBox2.Text;             vSalary = Convert .ToInt32(textBox3.Text);             SqlCommand cmd = new SqlCommand ( "P1" , conn);             cmd.CommandType = CommandType .StoredProcedure;             cmd.Parameters.AddWithValue( "@vEmpno" , vEmpno);             cmd.Parameters.AddWithValue( "@vEname" , vEname);             cmd.Parameters.AddWithValue( "@vSalary" , vSalary);             cmd.ExecuteNonQuery();             MessageBo

Coding for ADO.NET Online Method.

This post is for the Online Method Database (Connected Approach) in .NET Framework. Following are the three methods to do the same: OLEDB Method OleDbConnection vConn = new OleDbConnection( "provider=microsoft.jet.oledb.4.0; data source=c:\\MyDatabase.mdb" );             vConn.Open();              int vEmpno;             String vEname;             int vSalary;             //insert command             //Pick all the values from the textboxes             vEmpno = Convert.ToInt32(textBox1.Text);             vEname = textBox2.Text;             vSalary = Convert.ToInt32(textBox3.Text);   String vStr1 = "insert into Emp (Empno, Ename, Salary) values (" + vEmpno + ",'" + vEname + "'," + vSalary + ")" ;             OleDbCommand vComm = new OleDbCommand(vStr1, vConn);             vComm.ExecuteNonQuery();             MessageBox.Show( "Record Saved" );             vConn.Close(