using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication4
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private WindowsApplication4.DataSet1 dataSet11;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet11 = new WindowsApplication4.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=BOOMMBAAA;initial catalog=Northwind;integrated security=SSPI;persist " +
"security info=False;workstation id=BOOMMBAAA;packet size=4096";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Orders", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("OrderID", "OrderID"),
new System.Data.Common.DataColumnMapping("OrderDate", "OrderDate"),
new System.Data.Common.DataColumnMapping("ShipName", "ShipName")})});
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(120, 51);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(120, 97);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(120, 143);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "Refresh";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 48);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "Order Id";
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 100);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "Order Date";
//
// label3
//
this.label3.Location = new System.Drawing.Point(248, 152);
this.label3.Name = "label3";
this.label3.TabIndex = 6;
this.label3.Text = "Ship Name";
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT OrderID, OrderDate, ShipName FROM Orders";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("sr-SP-Cyrl");
this.dataSet11.Namespace = "
http://www.tempuri.org/DataSet1.xsd";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 283);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label3,
this.label2,
this.label1,
this.button1,
this.textBox3,
this.textBox2,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (textBox1.Text != " ") //nije prazan text box
{
sqlConnection1.Open();
textBox2.Text =VratiIzBazeShipName(textBox1.Text);
textBox3.Text =VratiIzBazeOrderDate(textBox1.Text);
sqlConnection1.Close();
}
}
private string VratiIzBazeShipName(string ORderID)
{
int ordID1 = int.Parse ( ORderID );
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText =" SELECT ShipName FROM Orders WHERE OrderID=ordID1 ";
SqlDataReader dataReader = dataCommand.ExecuteReader();
string strShipName = "" ;
while(dataReader.Read())
{
strShipName = dataReader.GetString(8);
Console.Write(strShipName);
}
dataReader.Close();
return(strShipName);
}
private string VratiIzBazeOrderDate(string ORderID)
{
int ordID2 = int.Parse ( ORderID );
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText =" SELECT OrderDate FROM Orders WHERE OrderID=ordID2 ";
SqlDataReader dataReader = dataCommand.ExecuteReader();
string strOrderDate = "" ;
while(dataReader.Read())
{
strOrderDate = dataReader.GetDateTime(3).ToString();
Console.Write(strOrderDate);
}
dataReader.Close();
return(strOrderDate);
}
}
}
.........................................................................
Evo, ispravio sam ti kod, no, ako ti ponovno ne proradi, javi se pa da vidim da ti to isprogramiram od početka.
Ispravke ---
1. Konekciju sam otvorio u klik proceduri Refreš dugmeta i tu je i zatvorio --- nije u redu da je otvaraš odmah na početku aplikacije ... potrebno je da je otvaraš kada ti treba, te potom zatvoriš ;
2. Promenio sam ti deklaracije u metodama za isšitavanje, jer si imao dvostruke deklaracije, a u smislu datuma porudžbine i pogrešne tipove ( najpre tip string, a potom u petlji tip 'DataTime' strukture ),
3. Usput, imaš viška data adapter i data set koje niti ne koristiš --- ti koristiš ekstenzivan način pristupa Sql bazi uz upotrebu samo objekata tipa 'SqlCommand' i 'SqlConnection' --- dakle direktan pristup. No, ponekad od viška glava ne boli, sem što će ti kompajler ( ako nije drugačije podešen ) javiti 'warning' u smislu da si deklarisao varijable koje nikada ne koristiš.
4. Višak su ti i naredbe za ispis na konzolu, pošto ispisuješ na Windows formu !