PDA

View Full Version : عدم نمایش اطلاعات دیتابیس



sogoli321
شنبه 13 آبان 1385, 00:14 صبح
باسلام
اگر مقاله اموزشی آقای هاشمیان رو مطالعه کرده باشید در قسمت دیتا بیس یک مثالی رو زده که ارتباط دیتا بیس در sql با c# رو بیان کرده (مثال binding example) من تمام مراحلی رو که ایشان گفته رفتم وتمام کدهای اشاره شده را نوشتم ولی وقتی برنامه اجرا میشه هیچگونه اطلاعاتی از دیتا بیس و محتویان جداول در برنامه نمایش داده نمی شود!! در ضمن زمان اجرا هیچ خطا و اروری نمی ده
در مجموع چرا وقتی از طریق کد اطلاعات یه بانک رو فراخونی می کنی ،انرا نمایش نمی ده... در صورتیکه برنامه خطایی را گزارش نمی دهد در ضمن اگر اطلاعات همین دیتا بیس را اگر بخوام با دیتاگرید نمایش بدم هیچ مشکلی نیست!!
ببخشید اگر واضح بیان نکردم چون تازه با دنیای سی شارپ اشنا شدم و روی کمکهای دوستان خیلی حساب باز کردم
:خجالت:
کمال تشکر را دارم

ARA
شنبه 13 آبان 1385, 09:11 صبح
با F11 یا ّF10 مرحله مرحله trace کن تا شاید بتئنی کدوم مرحله درست انجام میشه

sogoli
شنبه 13 آبان 1385, 10:04 صبح
در قسمت program.cs دستور زیر پیغام خطا می ده :
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
نمی دونم علتش چیه...
سورس برنامه :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BindingExample
{
public partial class Form1 : Form
{
// Constant strings
private const string _CommandText=
"SELECT authors.au_id,au_lname,au_fname"+
"title.title_id,title,price"+
"JOIN titleauthor ON authors.au_id = " +
"titleauthor.au_id " +
"JOIN titles ON titleauthor.title_id = " +
"titles.title_id " +
"ORDER BY au_lname, au_fname";
private const string _ConnectionString =
"server=localhost;database=test;"+
"user id=NT AUTHORITY\SYSTEM;password=;";
// Declare global objects...
SqlConnection objConnection;
SqlDataAdapter objDataAdapter;
DataSet objDataSet;
DataView objDataView;
CurrencyManager objCurrencyManager;

public Form1()
{
objConnection = new SqlConnection(_ConnectionString);
objDataAdapter = new SqlDataAdapter(_CommandText, objConnection);
InitializeComponent();
}
private void FillDataSetAndView()
{
// Initialize a new instance of the DataSet object...
objDataSet=new DataSet();
// Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet,"authors");
// Set the DataView object to the DataSet object...
objDataView=new DataView(objDataSet.Tables["authors"]);
// Set our CurrencyManager object
// to the DataView object...
objCurrencyManager=(CurrencyManager)(this.BindingC ontext[objDataView]);
}
private void BindFields()
{
// Clear any previous bindings...
txtFirstName.DataBindings.Clear();
txtFirstName.DataBindings.Clear();
txtBookTitle.DataBindings.Clear();
txtPrice.DataBindings.Clear();
// Add new bindings to the DataView object...
txtFirstName.DataBindings.Add("text",objDataView, "au_lname");
txtFirstName.DataBindings.Add("Text",objDataView, "au_fname");
txtBookTitle.DataBindings.Add("Text",objDataView, "title");
txtPrice.DataBindings.Add("Text",objDataView, "price");
// Display a ready status...
ToolStripStatusLabel1.Text = "Ready";
}
private void ShowPosition()
{
// Always format the number
// in the txtPrice field to include cents
try
{
txtPrice.Text=Decimal.Parse(txtPrice).ToString("##0.00");
}
catch(System.Exception e)
{
txtPrice.Text = "0";
txtPrice.Text =Decimal.Parse(txtPrice.Text).ToString("##0.00");
}
// Display the current position
// and the number of records
txtRecordPosition.Text =(objCurrencyManager.Position + 1) +" of " + objCurrencyManager.Count;
}

private void Form1_Load(object sender, EventArgs e)
{
// Add items to the combo box...
cboField.Items.Add("Last Name");
cboField.Items.Add("First Name");
cboField.Items.Add("Book Title");
cboField.Items.Add("Price");
// Make the first item selected...
cboField.SelectedIndex = 0;
// Fill the DataSet and bind the fields...
FillDataSetAndView();
BindFields();
// Show the current record position...
ShowPosition();
}

private void btnMoveFirst_Click(object sender, EventArgs e)
{
objCurrencyManager.Position=0;
ShowPosition();

}

private void btnMovePrevious_Click(object sender, EventArgs e)
{
// Move to the previous record...
objCurrencyManager.Position -= 1;
// Show the current record position...
ShowPosition();
}

private void btnMoveNext_Click(object sender, EventArgs e)
{
// Move to the next record...
bjCurrencyManager.Position += 1;
//Show the current record position...
ShowPosition();
}

private void btnMoveLast_Click(object sender, EventArgs e)
{
// Set the record position to the last record...
objCurrencyManager.Position =
objCurrencyManager.Count - 1;
// Show the current record position...
ShowPosition();
}
}


}
}

ARA
شنبه 13 آبان 1385, 16:18 عصر
میتونی خطا رو هم بگذاری تا بشه کمک کرد
البته اگه بلد بودیم D:

sogoli
یک شنبه 14 آبان 1385, 08:12 صبح
همانطوری که قبلا گفتم هیچ خطایی برنامه نمی ده و اجرا می شه ... ولی اطلاعات دیتابیس در دیتاگریدها و تکس باکسها نمایش داده نمی شه...

ARA
سه شنبه 23 آبان 1385, 18:47 عصر
خوب فایل ها رو زیپ کن و اینجا
بگذار

majid325
سه شنبه 23 آبان 1385, 19:03 عصر
بانک چیه؟ ........Access یا SQL !

البته دیدم که از SqlConnection استفاده کردی ...خواستم مطمئن بشم.