I want to retrieve the Record with Imagecolumn also and also want to stretch the Imagelayout for one example i was got success but for other i have problem and error is rise which is error=Child list for field Student cannot be created .please help me about this topic i will very thank full to you all my friends my EggheadhCafe friends
public sealed class DAC
{
// Create Class variabl
private static SqlDataAdapter StudentDataAdapter = CreateSutdentDataAdapter();
public static SqlDataReader GetCompanyInformation()
{
SqlDataReader reader;
string sql = "SELECT * FROM Student ";
using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
{
reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
}
return reader;
}
// Creat the Data Addapter
private static SqlDataAdapter CreateSutdentDataAdapter()
{
// Create the Select Statement and create Insert Update and Delete query
string gettSQL = "SELECT * FROM Student";
string insertSQL = "INSERT INTO Sutdent(StudentID, FirstName,LastName,Gender,GPA,MyImage)" +
"VALUES (@StudentID,@FirstName,@LastName,@Gender.@GPA,@MyImage)";
string updateSQL = "UPDATE Student SET StudentName=@StudentName, LastName=@LastName,Gender=@Gender,GPA=@GPA, MyImage=@MyImage";
string deleteSQL = "DELETE FROM Student WHERE StudentID=@StudentID";
// For Lager Programing (DATABASE) using StorProceduer not the string type
// Now create the SQLDATAAdapeter
SqlDataAdapter dataAdapter = new SqlDataAdapter();
// now Create the DataAdapter to Poputlate our GridView or DisconnectedDataset using SqlCommand
// In SelectCommand passing SQlCommand and in SQlCommand passing to varaible namely string sql query and the Connection of the Database
dataAdapter.SelectCommand = new SqlCommand(gettSQL, ConnectionManager.GetConnection());
//now in same way create the InsertCommand also
dataAdapter.InsertCommand = new SqlCommand(insertSQL, ConnectionManager.GetConnection());
// Create or Add the Parameter of th dataAdapter for InsertCommand
dataAdapter.InsertCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
dataAdapter.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25 ).SourceColumn = "FirstName";
dataAdapter.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25 ).SourceColumn = "LastName";
dataAdapter.InsertCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender";
dataAdapter.InsertCommand.Parameters.Add("@GPA", SqlDbType.Float ).SourceColumn = "GPA";
dataAdapter.InsertCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage";
//Create the UpdateCommand and the Parameters
dataAdapter.UpdateCommand = new SqlCommand(updateSQL, ConnectionManager.GetConnection());
dataAdapter.UpdateCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
dataAdapter.UpdateCommand.Parameters.Add("@FirstName", SqlDbType.VarChar,25 ).SourceColumn = "FirstName";
dataAdapter.UpdateCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 25 ).SourceColumn = "LastName";
dataAdapter.UpdateCommand.Parameters.Add("@Gender", SqlDbType.VarChar ,1).SourceColumn = "Gender";
dataAdapter.UpdateCommand.Parameters.Add("@GPA", SqlDbType.Float ).SourceColumn = "GPA";
dataAdapter.UpdateCommand.Parameters.Add("@MyImage", SqlDbType.VarBinary).SourceColumn = "MyImage";
dataAdapter.DeleteCommand = new SqlCommand(deleteSQL, ConnectionManager.GetConnection());
dataAdapter.DeleteCommand.Parameters.Add("@StudentID", SqlDbType.Int).SourceColumn = "StudentID";
// last return dataAdapter
return dataAdapter;
}
// Creating the Table Schima
private static void DefinestudentTableSchema(DataTable table)
{
DataColumn StudentIDColumn = table.Columns.Add("StudentID", typeof(string));
StudentIDColumn.AllowDBNull = false;
table.PrimaryKey = new DataColumn[] { StudentIDColumn };
DataColumn StudentFirstName = table.Columns.Add("FirstName", typeof(string));
StudentFirstName.MaxLength = 150;
DataColumn StudentLastName = table.Columns.Add("LastName", typeof(string));
StudentLastName.MaxLength = 150;
DataColumn StudentGender = table.Columns.Add("Gender", typeof(char));
//StudentGender.MaxLength = 10;
DataColumn StudentGPA = table.Columns.Add("GPA", typeof(float));
DataColumn StudentImage = table.Columns.Add("MyImage", typeof(Byte[]));
//Create the DataSet
}
private static DataSet CreateStudentTrackerDataSet()
{
DataSet StudentTrackerDataSet = new DataSet();
DataTable StudentTable = StudentTrackerDataSet.Tables.Add("Student");
DefinestudentTableSchema(StudentTable);
return StudentTrackerDataSet;
}
//GetDataFunction that Populat the Gridview
public static DataSet GetData()
{
DataSet StudentTrakerDataSet = CreateStudentTrackerDataSet();
StudentTrakerDataSet.EnforceConstraints = false;
StudentDataAdapter.Fill(StudentTrakerDataSet.Tables["Student"]);
StudentTrakerDataSet.EnforceConstraints = true;
return StudentTrakerDataSet;
}
And my .CS Code is
public partial class AditStudent : Form
{
// Creat the Class variabl Dataset to track the Student
private DataSet StudentTrackerDataset;
public AditStudent()
{
InitializeComponent();
// In the constructor of the AditStudent form we are puting the DataEvenhandler
dataGridView1.DataError += DataGridView1_DataError;
StudentTrackerDataset = ProjectOfSchool.DataAccessLayer.DAC.GetData();
// Problem is at here which tell us that Child list for field Student cannot be created.
dataGridView1.DataMember = "Student";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
if (dataGridView1.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)dataGridView1.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;}
DataTable StudentTable = StudentTrackerDataset.Tables["School"];
dataGridView1.DataSource = StudentTable;
}
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// creating DataErrorHandler and methods of Datagridview1
// string meassage by string farmaating to show how the error will look like for the Error in the Datagrid in from columan 0 and in row 1:2 multidimintional array type
string message = string.Format("Error in {0} columan in row {1}:{2}", e.ColumnIndex, e.RowIndex, e.Exception.Message);
// now show the Error using messagebox
MessageBox.Show(message, "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Error is that When i run the form then Child list for field Student cannot be created. is shown . And Yes in same way when i display the record using this code like
then no error and record is shown as i required Please tell me about how to fix this problem my friend
string sql = "SELECT * FROM Student";
using (SqlCommand Command = new SqlCommand(sql, ProjectOfSchool.DataAccessLayer.ConnectionManager.GetConnection()))
{
SqlDataAdapter ds = new SqlDataAdapter();
ds.SelectCommand = Command;
DataSet Ds = new DataSet();
ds.Fill(Ds, "Student");
dataGridView1.DataSource = Ds;
dataGridView1.DataMember = "Student";
for (int i = 0; i < dataGridView1.Columns.Count; i++)
if (dataGridView1.Columns[i] is DataGridViewImageColumn)
{
((DataGridViewImageColumn)dataGridView1.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
break;
}
Please i am waiting for your replay and sorry for my attendance in Eggheadhcafe