Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i'm new to asp.net and i'm doing this project for my academics. please help

P.S: currently i'm getting an error and it is "you must provide an initializer in a fixed or using statement declaration"

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=JIMMY-PC;initial Catalog=jimmy;Integrated Security=true");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("home.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo"),con) //Error here
        {
            //How to store the selected field?
        }
        using (SqlCommand cmd = new SqlCommand("Insert into Emplo ([Name],[Password],[Designation],[Dept],[D O B],[Sex],[Address]) values (@Name,@pass,@des,@dept,@DOB,@Sex,@Address)", con))
        {
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
            //Here i need to add the selected number from the previous statement and insert here to the current row
            cmd.Parameters.Add("@des", SqlDbType.VarChar).Value = TextBox2.Text;
            cmd.Parameters.Add("@dept", SqlDbType.VarChar).Value = TextBox3.Text;
            cmd.Parameters.Add("@DOB", SqlDbType.SmallDateTime).Value = TextBox4.Text;
            cmd.Parameters.Add("@Sex", SqlDbType.VarChar).Value = RadioButton1.SelectedItem.Text;
            cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox5.Text;
            string sa = CreateRandomPassword(6);
            cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = sa;
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('Registered successfully......!')</script>");
            Response.Write("<script>alert('Password is  " + Server.HtmlEncode(sa.ToString()) + "')</script>");
            
        }
        con.Close();
    }
    public static string CreateRandomPassword(int PasswordLength)
        { 
        string allowdChars = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = allowdChars.Length;
        for (int j = 0; j < PasswordLength; j++)
        {
            chars[j] = allowdChars[(int)((allowdChars.Length) * randNum.NextDouble())];
        }
         return new string(chars);       
        }
    }
Posted

1 solution

Check your brackets!
Change this:
C#
using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo"),con)
To this:
C#
using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo",con))
 
Share this answer
 
Comments
Jimmy-IN 27-Apr-15 5:34am    
@originalgriff how can i retrieve the value selected by select statement? where it will be stored? in cnd?
Jimmy-IN 27-Apr-15 5:41am    
how to store the selected field in a string
using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo",con))
{
//How to store the selected field?
}
OriginalGriff 27-Apr-15 5:51am    
SELECT queries don't store; they retrieve information from the DB.
To store, you need an INSERT (for new rows) as in your example code, or UPDATE (to change existing rows)

If you mean "how do I access the returned stuff?" there are two ways.
Has your course covered SqlDataReader or SqlDataAdapter yet?
Jimmy-IN 27-Apr-15 6:30am    
using (SqlCommand cnd = new SqlCommand("select LAST EmpID from Emplo",con))
//ERROR: Invalid column name 'LAST'.
{
con.Open();
SqlDataReader dr = cnd.ExecuteReader();
int x=Convert.ToInt32(dr);
String result = String.Format("a{0:0000}",x);
Response.Write("<script>alert('Your Employee ID is " + Server.HtmlEncode(result.ToString()) + "')</script>");
}
OriginalGriff 27-Apr-15 7:38am    
SQL doesn't support LAST - and it wouldn't work anyway as without a specified order to work with SQL is at liberty to return rows in whatever order it feels like - so the order can be different in consecutive SELECTs on the same data.
To return one specific row, try:
SELECT TOP 1 EmpId FROM Emplo ORDER BY EmpId DESC

But I'm not sure where you got that reader code from: it's...um....different...
Why would you assume that casting a SqlDataReader to an integer would give you anything useful? :laugh:
Try this:
---
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 EmpId FROM Emplo ORDER BY EmpId DESC",con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
int x = (int) dr["EmpId"];
string result = string.Format("a{0:0000}",x);
Response.Write("<script>alert('Your Employee ID is " + Server.HtmlEncode(result) + "')</script>");
}
}
}
---
Or, if you only want one, integer value:
---
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 EmpId FROM Emplo ORDER BY EmpId DESC",con))
{
int x = cmd.ExecuteScalar();
string result = string.Format("a{0:0000}",x);
Response.Write("<script>alert('Your Employee ID is " + Server.HtmlEncode(result) + "')</script>");
}
---

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900