|
private void findbutton_Click(object sender, EventArgs e)
{
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
try
{
// Open connection to the database
string ConnectionString = "server=INDUS-SERVER;uid=sa;pwd=victory;database=northwind";
con = new SqlConnection(ConnectionString);
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
string CommandText = "SELECT FirstName, LastName" + "FROM Employees" + "WHERE (LastName LIKE @Find)";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
// Add LastName to the above defined paramter @Find
cmd.Parameters.Add(
new SqlParameter(
"@Find", // The name of the parameter to map
System.Data.SqlDbType.NVarChar, // SqlDbType values
20, // The width of the parameter
"LastName")); // The name of the source column
// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;
// Execute the query
rdr = cmd.ExecuteReader();
// Fill the list box with the values retrieved
lblFound.Items.Clear();
while (rdr.Read())
{
lblFound.Items.Add(rdr["FirstName"].ToString() + " " + rdr["LastName"].ToString());
}
}
catch (Exception ex)
{
// Print error message
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (rdr != null)
rdr.Close();
if (con.State == ConnectionState.Open)
con.Close();
}
|
|
|
|
|
hi
Well there are couple of things that you are not doing right in your code. Your comments are interfering with your code. Your code is creaming "Put me inside a Function"
You have used unnecessary things in your code. if you can try to write your code like this
public SqlDataReader GetData()
{
SqlDataReader rd;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
string ConnectionString = "server=INDUS-SERVER;uid=sa;pwd=victory;database=northwind";
con = new SqlConnection(ConnectionString);
cmd.CommandText = "SELECT FirstName, LastName" + "FROM Employees" + "WHERE (LastName LIKE @Find)";
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@Find", SqlDbType.NVarChar, 20, "LastName"));
cmd.Parameters["@Find"].Value = txtFind.Text;
try
{
con.Open();
rd = cmd.ExecuteReader();
rd.Close();
con.Close();
}
catch (SqlException e)
{
Messagebox.show(e.Message);
}
return rd;
}
}
}
Now things are Better, because Previously you just opened the Connection where you were not supposed to, remember that you have to connect to the Database when you want to execute something. after you are done you close it, and now this function returns a reader, then you can iterate through your records or your can just choose to send the data into the datatable and bind the control.
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
Stil i'm getting error..i will do one thing just i will paste all the code.please resolve it...
Code:
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 DataReaderWith2Tables
{
public partial class Form1 : Form
{
private System.Windows.Forms.ListBox lblFound = null;
private System.Windows.Forms.Button findButton;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtFind = null;
//private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void findbutton_Click(object sender, EventArgs e)
{
SqlDataReader rdr;
SqlConnection con;
SqlCommand cmd = new SqlCommand();
string ConnectionString = "server=INDUS-SERVER;uid=sa;pwd=victory;database=northwind";
con = new SqlConnection(ConnectionString);
cmd.CommandText = "SELECT FirstName, LastName" + "FROM Employees" + "WHERE (LastName LIKE @Find)";
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter("@Find", System.Data.SqlDbType.NVarChar, 20, "LastName"));
cmd.Parameters["@Find"].Value = txtFind.Text;
try
{
con.Open();
rdr = cmd.ExecuteReader();
rdr.Close();
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
return rdr;
}
}
}
|
|
|
|
|
I think you are closing the rdr (rdr.Close()) in try block and then you are trying to return rdr (return rdr). Please check it.
|
|
|
|
|
i'm trying on an application in which i want the user to move the components on the form.(a checker game , the player should move the object on the board)could any one suggest me on this . thank you!
|
|
|
|
|
When the mouse left button is clicked then start moving and when right mouse button is clicked then stop moving. This is a varibale.
Now on mouse move event if start moving is true then capture the mouse position and assign the location value to mouse current point. at the end call invalidate() Fn.
Be Happy
Syed Shahid Hussain
|
|
|
|
|
thanks for your reply,got the idea,is there any references which can be used...
|
|
|
|
|
How can i change the size of a textbox(Which exists on the window) while resizing the window size?
Thank you
|
|
|
|
|
Use Anchor property for that textbox.
|
|
|
|
|
Hi,
can you guys help me out .. what exactly i need to install, i wanted to connect Oracle server 7.3.4 with OLEDB interface?
regards
ranganadh kodali
Kodali
|
|
|
|
|
Hi! I have this application what I build and I want to put automatic updates for the application so the user will have the latest version of the application. I found in the properties menu, the publish tab, where it says about automatic updates and publish the application. So now I want to add a button to the application, so when the user will click the button it will check for the latest version and if there is a version newer then the one they are using in the moment, the app will be updated to the latest version. Any help with this how I can solve? Thanks ahead
|
|
|
|
|
Please google search for ClickOnce deployment type. In ClickOnce the application checks for updates as soon as it is started so that ensuring the usage of latest version.
Regards,
|
|
|
|
|
I used below project source code to make MHT,
http://www.codeproject.com/KB/files/MhtBuilder.aspx[^]
Below strings are problem.
1:
"<link[^>]+?href\s*=\s*(?<key>('|"")*(?<value>[^'"">]+)('|"")*)"
2:
"<i*frame[^>]+?src\s*=\s*(?<key>['""]{0,1}(?<value>[^'""\\>]+)['""]{0,1})"
3:
"(?<attrib>@import\s|\S+-image:|background \s*?(url)*['""(]{1,2}" & _
"(?!http)\s*/(?<url>[^""')]+)['"")]{1,2}"
In some links it takes between 2 and 10 minutes. it usually happens in external links.
WHY?
Thanx...
I want to fly but I don't have wings
modified on Wednesday, August 6, 2008 6:44 AM
|
|
|
|
|
I don't know, but I suspect the doubled-quotes, try \" instead.
For getting hrefs I use:
"href\\s*=\\s*(?'quot'[\"'])(?'href'.*?)((?'sep'#)(?'id'.*?))?\\k'quot'"
|
|
|
|
|
|
yes sir, I solved click problem in vb code
I didn't use cssPattern in regex and it works fast and I have no any problems on clicks...
Only one problem left. CSS
I want to fly but I don't have wings
modified on Wednesday, August 6, 2008 6:44 AM
|
|
|
|
|
Hi,
i am using DataGridView, kindly let me know or give me examples link that how may I get row ID from KeyPress event?
Thanks in advance
(Riaz)
modified on Tuesday, August 5, 2008 2:31 AM
|
|
|
|
|
Hi Riaz;
Are you mean get the Row Index? If so, hope this help
this.dataGridView1.KeyPress +=new KeyPressEventHandler(dataGridView1_KeyPress);<br />
<br />
private void dataGridView1_KeyPress(object sender,KeyPressEventArgs e)<br />
{<br />
int i;<br />
i = ((DataGridView)sender).CurrentRow.Index;<br />
}
regard
cocoonwls
|
|
|
|
|
Hi,
I want to develop some fancy controls like buttons, checkboxes. or can say extend the look of existing controls that are available in Asp.net 3.5.
Is WPF appropriate for it or i use GDI+ to develop my own controls.
|
|
|
|
|
Umm...Think you might be a bit confused. Asp.net is a web page technology. If you want to write or extend Asp.net controls, you have to do it in asp.net
WPF is the new client app user interface technology. It is nice and easy to write good looking custom controls in this, or modify the existing ones. But they are for Client apps.
GDI+ is the underlying technology that is used by Winforms. You can write custom controls with Winforms, and you can extend the existing ones. It's not as easy as in WPF, but it can be done. This is also for client apps only.
Silverlight is another web page technology that is related to WPF because it allows a subset of the XAML used by WPF. This also allows custom and extended controls. Silverlight is kind of like flash and requires a browser plugin to work.
(XAML is extensible application markup language, and is the XML style syntax used to define controls and layout in WPF and Silverlight).
Simon
|
|
|
|
|
Class2 inherits Class 1 and implements interface ISortable. How to write code for this in c#?
|
|
|
|
|
srisravani wrote: implements interface ISortable
Never heard of it.
|
|
|
|
|
Use IComparable to sort something.
Never heard of ISortable
|
|
|
|
|
Hi all,
In my application, i have one arraylist which will store graphicspath object (series of line), let said gp1 (contains points a1,a2,a3),gp2 (contains points b1,b2,b3),and gp3(contains points c1,c2,c3),. These graphic path will be drawn in one drawing area. I would like to ask that how can i know which graphics path was selected when mousedown event occur? Off cours, i have already get the particular mouse point,let said points b2.
And in this situation, does create a region have any help in my problem(i mean create a graphicpath region to determind whether the points is in the graphicspath object)?
any help or tips are welcome
regards
cocoonwls
|
|
|
|
|
Hi all,
I think i can use graphicspath.IsVisible(points) to determind whether the particular object was selected...
|
|
|
|