Friday, 15 December 2006

My .NET Articles...

I have always tried to write technical articles and I got some of my articles published on
sites like
www.codeguru.com, www.codeproject.com.
Here are those links.

Will keep on putting more articles.

Some inspirational links I found...

http://quotations.about.com/cs/inspirationquotes/a/Success1.htm

For SQL server questions...

http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html

Add, edit, delete datagridview.

After so many days. I was busy with Sister-In-Law's marriage. Yesterday I was not able to login. Something wierd was happening. Yesterday I was fignting with simple datagridview to add, edit and delete operations. I was able to to add and edit but delete? It took me 3-4 hours to make it work anf finally I did it. The following link helped me.

http://msdn2.microsoft.com/en-us/library/5s3ce6k8.aspx

It was simple thing to do but I was trying it with datatable. But actually you can do it with


public partial class Form1 : Form
{
private const string connectionString = "Database=MyDatabase;Server=localhost;User=sa;Password=sa;Enlist=false;";
private string sqlQuery;
private SqlConnection connection;
private SqlCommand command;
private SqlDataAdapter adapter;
private SqlCommandBuilder builder;
private DataSet ds;
private DataTable userTable;
private string[] fields = { "StatusId", "ADUserId", "Email", "FirstName",
"LastName" , "JobTitle", "DepartmentId", "Phone", "Fax",
"Mobile", "UniqueKey", "IsDeleted", "CreatedByUserId", "CreatedOn",
"ModifiedByUserId" };
public Form1()
{
InitializeComponent();
SetDataObjects();
btnUpdate.Enabled = false;
}
private void SetDataObjects()
{
sqlQuery = "SELECT UserID, StatusId, ADUserId, Email, FirstName, LastName, "
+ " JobTitle, DepartmentId, Phone, Fax, Mobile, UniqueKey,"
+ " IsDeleted, CreatedByUserId, CreatedOn, ModifiedByUserId"
+ " FROM [User]";
connection = new SqlConnection(connectionString);
command = new SqlCommand(sqlQuery, connection);
adapter = new SqlDataAdapter(command);
builder = new SqlCommandBuilder(adapter);
ds = new DataSet("MainDataSet");
}
private void btnLoad_Click(object sender, EventArgs e)
{
btnLoad.Enabled = false;
try
{
if (userTable != null)
{
userTable.Clear();
}
userDataGridView.DataSource = null;
userDataGridView.Rows.Clear();
userDataGridView.Refresh();
connection.Open();
adapter.Fill(ds, "User");
userTable = ds.Tables["User"];
foreach (string field in fields)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
column.ValueType = userTable.Rows[0][field].GetType();
column.DataPropertyName = field;
column.HeaderText = field.ToUpper();
column.Name = field;
column.ReadOnly = false;
column.SortMode = DataGridViewColumnSortMode.Automatic;
column.Visible = true;
switch (field.ToLower())
{
case "aduserid":
case "email":
case "firstname":
case "lastname":
case "jobtitle":
case "departmentid":
case "phone":
case "fax":
case "mobile":
break;
case "statusid":
userTable.Columns[field].DefaultValue = 1;
column.Visible = false;
break;
case "userid":
column.Visible = false;
break;
case "uniquekey":
userTable.Columns[field].DefaultValue = 1;
column.Visible = false;
break;
case "isdeleted":
userTable.Columns[field].DefaultValue = 0;
column.Visible = false;
break;
case "createdbyuserid":
userTable.Columns[field].DefaultValue = 1;
column.Visible = false;
break;
case "createdon":
userTable.Columns[field].DefaultValue = DateTime.Now;
column.Visible = false;
break;
case "modifiedbyuserid":
userTable.Columns[field].DefaultValue = 1;
column.Visible = false;
break;
}
userDataGridView.Columns.Add(column);
}
userDataGridView.DataSource = userTable.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
btnLoad.Enabled = true;
}
finally
{
connection.Close();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
userDataGridView.ReadOnly = false;
btnAdd.Enabled = false;
btnUpdate.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
connection.Open();
adapter.Update(userTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
btnAdd.Enabled = true;
btnLoad.Enabled = true;
btnUpdate.Enabled = true;
connection.Close();
}
}

// Was simeple but I took little bit more time.
private void btnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to delete selected record(s)?",
"Delete Warning", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
== DialogResult.Yes)
{
try
{
connection.Open();
int cnt = userDataGridView.SelectedRows.Count;
for (int i = 0; i <> 0 &&
this.userDataGridView.SelectedRows[0].Index !=
this.userDataGridView.Rows.Count - 1)
{
this.userDataGridView.Rows.RemoveAt(
this.userDataGridView.SelectedRows[0].Index);
}
}
adapter.Update(userTable);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
connection.Close();
btnLoad.Enabled = true;
}
}
}


}

Friday, 8 December 2006

Adding knowledge links

Here are some nice links again..
ADO.NET and data persistent framework:
http://www.adapdev.com/downloads/WIDNUG-0511.ppt#1

Tackle Data Concurrency Exceptions Using the DataSet Object:
http://msdn.microsoft.com/msdnmag/issues/03/04/DataConcurrency/

Fighting with SqlDataAdapter.

Yesteray, I was relpying one forum on codeguru. That guys asked me the code. How to add a row to a datatable and how to reflect the changes back to datasource. I was trying a lot to get it done. Searched many sites but I was not able to do it. http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q310347

One of my friend who is SQL MCP came for tea and he also got involved in the same. We tried with SQL profiler. Came to know that when I call dataAdapter.Update(ds); database was not getting hit. Again we searched a lot an then we followed these steps.

  1. Remove all rows from table which is populated from dataset filled by adapter.
  2. Add a row with values.
  3. Call update.

This worked well but we were loosing all data. So AcceptChanges() method helped us. I was using this method after adding a row. This makes the impression that eveything is fine and no need to hit database. So what we did is create a new row, add values to respective columns, then call AccpetChanges() for dataset and datatable and then call AddRow() and then adapter.Update..

husssssh.. it worked at last.. Was happy to see that.

Here is the sample code.

string sqlQuery = "SELECT StatusId, ADUserId, Email, FirstName, LastName, JobTitle, DepartmentId, Phone, Fax, Mobile, UniqueKey, IsDeleted, CreatedByUserId, CreatedOn, ModifiedByUserId FROM [User]";

string connectionString = "Database=DbUsers;Server=localhost;User=user;Password=pwd;Enlist=false;";SqlCommand cmd;

SqlDataAdapter adpt;

DataSet ds = new DataSet();

using (SqlConnection conn = new SqlConnection(connectionString))

{

conn.Open();

cmd = new SqlCommand(sqlQuery, conn);

cmd.CommandType = CommandType.Text;

adpt = new SqlDataAdapter(cmd);

SqlCommandBuilder builder = new SqlCommandBuilder(adpt);

adpt.Fill(ds, "User");

if (ds.Tables[0].Rows.Count > 0)

{ DataRow row = ds.Tables["User"].NewRow();

row["StatusId"] = 1;

row["ADUserId"] = 5;

row["Email"] = jayantdotnet@gmail.com;

row["FirstName"] = "Jayant";

row["LastName"] = "Kulkarni";

row["JobTitle"] = "Chief Architect";

row["DepartmentId"] = 1;

row["Phone"] = "12334";

row["Fax"] = "122333";

row["Mobile"] = "22314234";

row["UniqueKey"] = 120;

row["IsDeleted"] = 0;

row["CreatedByUserId"] = 1;

row["CreatedOn"] = DateTime.Now;

row["ModifiedByUserId"] = 1;

ds.Tables["User"].AcceptChanges();

ds.AcceptChanges();

ds.Tables["User"].Rows.Add(row);

adpt.Update(ds.Tables["User"]);

}

}

Thursday, 7 December 2006

Marathi nice links

As posted in previous article, Prashant N. Mhatre is having very nice collection on yahoo.
http://groups.yahoo.com/group/prashant_n_mhatre/files/

Marathi articles....

Here are some marathi articles, poems from Prashant Mhatre, maiboli etc. Also some stuff is from newpapers like Loksatta..










































.NET links and some other stuff...

Yesterday, was busy with writing Exception Management white paper. Submitted the same. Today I got a nice article for Determining .NET Assembly and Method References.
Here are the links.
http://msdn.microsoft.com/msdnmag/issues/06/03/TestRun/#bottom.
http://www.codeproject.com/csharp/sdilreader.asp

Excellent link for getting connection strings to different databases.
http://www.connectionstrings.com/

For BizTalk people, here are samples available.
http://www.biztalkgurus.com/biztalk-server-2006.aspx

From microsoft, there is virtual lab at this location.
https://www.microsoft.com/resources/virtuallabs/step3-msdn.aspx?LabID=e423099b-b71b-44f6-bd63-b11170f1e0cf&CID=110834&Token=D0Brd6OxTtLrB9Ts6R8n08_MUvNvOT7w&DB=1
You will need to register with your passport.

There is online telephone directory of BSNL. Visit... http://www.bsnl.co.in/onlinedirectory.htm

Imagine if people all over the world mobilized to replace one billion standard incandescent light bulbs with energy-efficient compact fluorescent (CFL) light bulbs. What would that mean? It would mean that those people would save money each month on their electricity bill. It would mean they would save enough energy to light tens of millions of homes for a year. It would mean the prevention of greenhouse gases equivalent to the annual emissions of millions of cars.
visit: http://www.onebillionbulbs.com/Stats/Country/IN