Thursday, 21 December 2006
After so long..
Today I found one nice link which used to bind 2d array to datagrid.
http://www.codeproject.com/cs/database/BindArrayGrid.asp
Today started looking at .NET 3.0 for new project.
Here is nice link for the same.
http://www.netfx3.com/
Monday, 18 December 2006
Comments on article.
for me and I started searching on msdn2. Surprisingly I came to know that Microsoft does not recommend to derive custom exception classes from System.ApplicationException rarther is says that you should derive it directly from System.Exception. In .NET 1.1 it was exactly reverse. But this approach does not add significant value addition.
Here is link from MSDN2.
http://msdn2.microsoft.com/en-us/library/ms229005.aspx
While browsing this link I was really amazed to see so much of information. I am trying to make it at one place..
There are many good articles on usage on msdn2.
Some of them are like..
Guidelines for Names:
http://msdn2.microsoft.com/en-us/library/ms229002.aspx
Type Design Guidelines:
http://msdn2.microsoft.com/en-us/library/ms229036.aspx
Member Design Guidelines:
http://msdn2.microsoft.com/en-us/library/ms229059.aspx
Designing for Extensibility:
http://msdn2.microsoft.com/en-us/library/ms229028.aspx
Usage Guidelines:
http://msdn2.microsoft.com/en-us/library/ms229035.aspx
.NET Framework Class Library Reference:
http://msdn2.microsoft.com/en-us/library/d11h6832(VS.80).aspx
Asynchronous Programming Design Patterns:
http://msdn2.microsoft.com/en-us/library/ms228969.aspx
These are too good articles and everyone should read to expand their knowledgebase.
Friday, 15 December 2006
My .NET Articles...
sites like
www.codeguru.com, www.codeproject.com.
Here are those links.
- http://www.codeguru.com/csharp/csharp/cs_syntax/errorhandling/article.php/c12971/
- http://www.codeguru.com/csharp/.net/net_general/eventsanddelegates/article.php/c12933/
- http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c11367/
- http://www.codeguru.com/csharp/csharp/cs_syntax/inheritance/article.php/c10215/
- http://www.codeproject.com/Purgatory/NET_remoting_in_chat_app.asp
- http://www.codeproject.com/useritems/Events_and_Delegates.asp
- http://www.codeproject.com/useritems/Satellite_assemblies.asp
- http://www.csharphelp.com/archives2/archive438.html
Will keep on putting more articles.
Some inspirational links I found...
http://quotations.about.com/cs/inspirationquotes/a/Success1.htm
For SQL server questions...
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
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.
- Remove all rows from table which is populated from dataset filled by adapter.
- Add a row with values.
- 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
http://groups.yahoo.com/group/prashant_n_mhatre/files/