-
October 16th, 2011, 01:20 AM
#1
[RESOLVED] Restrict text input
hi,
i am new in C# , i have 2 text boxes in which the user will enter the department and name,
1- I want the application to retrict the user to only enter letters not numbers . This is my following code:
public string PMEWDirectorates_Insert(string PMEWDirectorate_Name, string PMEWDirectorate_Head)
{
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
// SqlDataAdapter da = new SqlDataAdapter();
try
{
con.ConnectionString = conString;
con.Open();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "PMEWDirectorates_Insert";
//da.SelectCommand = com;
com.Parameters.AddWithValue("@PMEWDirectorate_Name", PMEWDirectorate_Name);
com.Parameters.AddWithValue("@PMEWDirectorate_Head", PMEWDirectorate_Head);
com.ExecuteNonQuery();
return "0";
}
catch(Exception ex)
{
return ex.Message;
}
finally
{
con.Close();
// da.Dispose();
com.Dispose();
con.Dispose();
}
}
}
public partial class Parts_PMEWDirectorate : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_save_Click(object sender, EventArgs e)
{
btn_save.Enabled = false;
lookups insert = new lookups();
string result = insert.PMEWDirectorates_Insert(txt_DirName.Text.Trim(), txt_DirHead.Text.Trim());
if (result == "0")
{
lbl_msg.ForeColor = System.Drawing.Color.Blue;
lbl_msg.Text = "Successful";
}
else
{
lbl_msg.ForeColor = System.Drawing.Color.Red;
lbl_msg.Text = result;
}
btn_save.Enabled = true;
}
2- How can i put a grid view to view the records he entered?
public DataSet PMEWDirectorates_GetAll()
{
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
con.ConnectionString = conString;
con.Open();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "PMEWDirectorates_GetAll";
da.SelectCommand = com;
com.Parameters.AddWithValue("@ID", DBNull.Value);
com.ExecuteNonQuery();
da.Fill(ds);
return ds;
}
catch
{
return ds;
}
finally
{
con.Close();
ds.Dispose();
da.Dispose();
com.Dispose();
con.Dispose();
}
}
Thank you
-
October 16th, 2011, 03:03 AM
#2
Re: Restrict text input
Hello Nmohammed, the following code will allow only a-z and A-Z characters to be entered into the textbox. You'll need to create the KeyDown event for the textboxes, in order to utilize the code. In the future, I'd also like to suggest, for readability, to use [ code ] tags, on your code. Enjoy.
Code:
private void txtTextbox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key < Key.A) || (e.Key > Key.Z))
e.Handled = true;
}
Regards,
Quinn
If this post resolves your question, please make sure to flag post as resolved and please rate up this post. Thanks.
Last edited by QuinnJohns; October 16th, 2011 at 03:06 AM.
-
October 16th, 2011, 04:17 AM
#3
Re: Restrict text input
Thank you very much
Is there a solution for my second question, How can i put a grid view to view the records from the database?
Code:
public DataSet PMEWDirectorates_GetAll()
{
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
con.ConnectionString = conString;
con.Open();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "PMEWDirectorates_GetAll";
da.SelectCommand = com;
com.Parameters.AddWithValue("@ID", DBNull.Value);
com.ExecuteNonQuery();
da.Fill(ds);
return ds;
}
catch
{
return ds;
}
finally
{
con.Close();
ds.Dispose();
da.Dispose();
com.Dispose();
con.Dispose();
}
}
-
October 16th, 2011, 10:44 AM
#4
Re: Restrict text input
Well, assuming you have the control, usually you can just set the Datasource element in code, to that of the Dataset.
See the following:
Code:
DataSet ds = PMEWDirectorates_GetAll();
if (ds.Tables.Count > 0)
{
GridView.DataSource = ds;
GridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
Regards,
Quinn
If this post resolves your question, please make sure to flag post as resolved. Thanks.
Last edited by QuinnJohns; October 16th, 2011 at 10:55 AM.
-
October 17th, 2011, 12:05 AM
#5
Re: Restrict text input
where shall i include this code?
-
October 17th, 2011, 12:30 AM
#6
Re: Restrict text input
Wherever, you intend on populating this content into the control. So, a Form_Load for example.
-
October 17th, 2011, 12:40 AM
#7
Re: Restrict text input
sorry to ask this question , but which control do you mean ? the gridview?
-
October 17th, 2011, 05:34 AM
#8
Re: Restrict text input
My public DataSet PMEWDirectorates_GetAll() is located in a .cs file called functions.cs and i'm writing my code in a .ascx.cs page
i want to display the results in a grid view in this page
-
October 17th, 2011, 06:50 PM
#9
Re: Restrict text input
Originally Posted by nmohammed
My public DataSet PMEWDirectorates_GetAll() is located in a .cs file called functions.cs and i'm writing my code in a .ascx.cs page
i want to display the results in a grid view in this page
You need to learn how to access a public function in another class, please see google for help.
Regards,
Quinn
-
October 18th, 2011, 02:01 AM
#10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|